Skip to content
This repository has been archived by the owner on Feb 6, 2020. It is now read-only.

Commit

Permalink
support display the local picture
Browse files Browse the repository at this point in the history
  • Loading branch information
iamcco committed Jan 24, 2016
1 parent e909327 commit 2991545
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 4 deletions.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ through a browser

![screenshot](./screenshot.gif)


### Installation

Using with [Vundle][Vundle]:
Expand Down Expand Up @@ -60,6 +59,10 @@ MarkdownPreviewStop command before using MarkdownPreview command
" move the cursor
```

### Changelog

* 2016/01/24: support display the local picture in markdown

--------------------------------------------------------------------------------

### 说明
Expand Down Expand Up @@ -112,4 +115,8 @@ MarkdownPreviewStop command before using MarkdownPreview command
" 更新预览
```

### Changelog

* 2016/01/24: 支持显示本地图片

[Vundle]: https://github.com/VundleVim/Vundle.vim
15 changes: 13 additions & 2 deletions autoload/mkdp.vim
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,15 @@ let g:mkdp_port = 8686
let g:mkdp_prefix = localtime()
let g:mkdp_bufs = {}
let s:path_to_server = expand('<sfile>:p:h') . "/server/server.py"
let g:mkdp_cwd = ''

"python/python3 import init
exec s:py_cmd . 'import vim'
exec s:py_cmd . 'import sys'
exec s:py_cmd . 'cwd = vim.eval("expand(\"<sfile>:p:h\")")'
exec s:py_cmd . 'sys.path.insert(0,cwd)'
exec s:py_cmd . 'from server import send'
exec s:py_cmd . 'import base64'

fun! mkdp#browserStart() abort "open browser and save the buffer number
if !has_key(g:mkdp_bufs, bufnr('%'))
Expand Down Expand Up @@ -105,10 +107,19 @@ fun! s:serverClose() abort "function for close the server
endfu

fun! s:browserStart() abort "function for opening the browser
let g:mkdp_cwd = expand('%:p:h')

" py2/py3 different resolve for str
if g:mkdp_py_version == 2
exec s:py_cmd . 'vim.command("let g:mkdp_cwd = \"" + base64.b64encode(vim.eval("g:mkdp_cwd")) + "\"")'
elseif g:mkdp_py_version == 3
exec s:py_cmd . 'vim.command("let g:mkdp_cwd = \"" + base64.b64encode(vim.eval("g:mkdp_cwd").encode("utf-8")).decode("utf-8") + "\"")'
endif

if s:mkdp_is_windows()
exec "silent !start " . g:mkdp_path_to_chrome . " http://127.0.0.1:" . g:mkdp_port . "/markdown/" . g:mkdp_prefix . bufnr('%')
exec "silent !start " . g:mkdp_path_to_chrome . " http://127.0.0.1:" . g:mkdp_port . "/markdown/" . g:mkdp_prefix . bufnr('%') . '?' . g:mkdp_cwd
else
call system(g:mkdp_path_to_chrome . " http://127.0.0.1:" . g:mkdp_port . "/markdown/" . g:mkdp_prefix . bufnr('%') . " &>/dev/null &")
call system(g:mkdp_path_to_chrome . " http://127.0.0.1:" . g:mkdp_port . "/markdown/" . g:mkdp_prefix . bufnr('%') . '?' . g:mkdp_cwd) . " &>/dev/null &")
endif
endfun

Expand Down
22 changes: 21 additions & 1 deletion autoload/server/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@
u'js' : u'application/javascript',
u'ico' : u'image/x-icon',
u'jpg' : u'image/jpg',
u'png' : u'image/png'
u'jpeg' : u'image/jpg',
u'png' : u'image/png',
u'gif' : u'image/gif',
u'bmp' : u'image/bmp',
u'all' : u'text/plain'
}
SOCKET_HEADER = u'HTTP/1.1 101 Switching Protocols\r\n'\
u'Upgrade: websocket\r\n'\
Expand Down Expand Up @@ -144,6 +148,22 @@ def doGet(conn, hItmes, path):
else:
conn.send(OK_HEADER.encode(ENCODING))
conn.close()
elif paths[1].startswith(u'image'):
p = base64.b64decode(path.split(u'?')[1].encode(ENCODING)).decode(ENCODING)
if os.path.exists(p):
ps = p.split(u'.')
if len(ps) >= 2:
mimeType = ps[len(ps)-1].lower()
else:
mimeType = u'all'
f = open(p, u'rb')
s = f.read()
f.close()
conn.send((MD_HEADER % (len(s), MIME_TYPE[mimeType])).encode(ENCODING) + s)
conn.close()
else:
conn.send(OK_HEADER.encode(ENCODING))
conn.close()
else:
conn.send(OK_HEADER.encode(ENCODING))
conn.close()
Expand Down
1 change: 1 addition & 0 deletions autoload/server/static/htmls/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<script src="/static/scripts/highlight.js"></script>
<script src="/static/scripts/marked.js"></script>
<script src="/static/scripts/TweenLite.min.js"></script>
<script src="/static/scripts/base64.min.js"></script>
</head>
<body>
<section class="container">
Expand Down
1 change: 1 addition & 0 deletions autoload/server/static/scripts/base64.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions autoload/server/static/scripts/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,26 @@
body = document.body;
html = document.querySelector('html');
mkdContainer = document.getElementById('js-markdown');

function getAbsPath(base, path) {
var bases = Base64.decode(base.slice(1)).split('/')
var paths = path.split('/');
if(/^https?:?/i.test(paths[0])) {
return path;
} else if(!/(^\..*)|(^\.\..*)/i.test(paths[0])) {
return '/image?' + Base64.encode(path);
} else {
for(var i = 0, len = paths.length; i < len; i++) {
if(paths[i] === '..') {
bases.pop();
} else if(paths[i] !== '.') {
bases.push(paths[i]);
}
}
}
return '/image?' + Base64.encode(bases.join('/'));
}

options = (function() {
var flagSign = '019600976811CE18D7D4F7699D774DFF', //md5 of the yuuko.cn
rFlagSign = flagSign.split('').reverse().join(''),
Expand Down Expand Up @@ -81,6 +101,7 @@
text = text.replace(flagSign, '');
result = aPoint;
}
href = getAbsPath(location.search, href);
return result + rImage.call(renderer, href, title, text);
};

Expand Down

0 comments on commit 2991545

Please sign in to comment.