Skip to content

Commit

Permalink
support PlantumlSave no arguments
Browse files Browse the repository at this point in the history
resolve weirongxu#11
  • Loading branch information
weirongxu committed Jun 1, 2018
1 parent 9fbc2d8 commit 2ac4f98
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 24 deletions.
13 changes: 9 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,23 @@ Open previewer webpage in browser, and watch current buffer
#### PlantumlStop
Stop watch buffer

#### PlantumlSave {filepath} [{format}]
#### PlantumlSave [{filepath}] [{format}]
Export uml diagram to file path
Available formats
> png, svg, eps, pdf, vdx, xmi,
> scxml, html, txt, utxt, latex
Example:
```
:e diagram.puml
:PlantumlSave
:PlantumlSave diagram.png
:PlantumlSave diagram.svg
```

### Variables
```
g:plantuml_previewer#plantuml_jar_path
```
#### `g:plantuml_previewer#plantuml_jar_path`
Custom plantuml.jar file path

If plant uml was installed by homebrew, you can add the following code to your `.vimrc` to use the version installed by homebrew:
Expand All @@ -52,6 +53,10 @@ au FileType plantuml let g:plantuml_previewer#plantuml_jar_path = get(
\)
```

#### `g:plantuml_previewer#save_format`
default `:PlantumlSave` format
Default: 'png'

## Related
* [vim-slumlord](https://github.com/scrooloose/vim-slumlord)
* [previm](https://github.com/kannokanno/previm)
50 changes: 34 additions & 16 deletions autoload/plantuml_previewer.vim
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ let s:is_win = has('win32') || has('win64') || has('win95')

let s:base_path = expand("<sfile>:p:h") . '/..'

let s:jar_path = s:base_path . '/lib/plantuml.jar'
let s:default_jar_path = s:base_path . '/lib/plantuml.jar'

let s:tmp_path = s:base_path . '/tmp'

Expand Down Expand Up @@ -43,9 +43,25 @@ function! plantuml_previewer#stop() "{{{
augroup END
endfunction "}}}

function! plantuml_previewer#jar_path() "{{{
function! s:is_zero(val) "{{{
return type(a:val) == type(0) && a:val == 0
endfunction "}}}

function! s:jar_path() "{{{
let path = get(g:, 'plantuml_previewer#plantuml_jar_path', 0)
return path == 0 ? s:jar_path : path
return s:is_zero(path) ? s:default_jar_path : path
endfunction "}}}

function! s:save_format() "{{{
return get(g:, 'plantuml_previewer#save_format', 'png')
endfunction "}}}

function! s:ext_to_fmt(ext) "{{{
return a:ext == 'tex' ? 'latex' : a:ext
endfunction "}}}

function! s:fmt_to_ext(fmt) "{{{
return a:fmt == 'latex' ? 'tex' : a:fmt
endfunction "}}}

function! s:run_in_background(cmd) "{{{
Expand All @@ -67,7 +83,7 @@ function! plantuml_previewer#refresh() "{{{
call writefile(content, s:viewer_tmp_puml_path)
let cmd = [
\ s:update_viewer_script_path,
\ plantuml_previewer#jar_path(),
\ s:jar_path(),
\ s:viewer_tmp_puml_path,
\ 'svg',
\ localtime(),
Expand All @@ -76,27 +92,29 @@ function! plantuml_previewer#refresh() "{{{
call s:run_in_background(cmd)
endfunction "}}}

function! plantuml_previewer#save_as(save_path) "{{{
let target_type = get(a:000, 1, 0)
let save_path = fnamemodify(a:save_path, ':p')
if target_type == 0
function! plantuml_previewer#save_as(...) "{{{
let save_path = get(a:000, 0, 0)
let target_format = get(a:000, 1, 0)
if s:is_zero(save_path)
let source_name = expand('%:t:r')
let save_path = printf("%s.%s", source_name, s:fmt_to_ext(s:save_format()))
else
let save_path = fnamemodify(save_path, ':p')
endif
if s:is_zero(target_format)
let ext = fnamemodify(save_path, ':e')
if ext == ''
let target_type = 'svg'
else
let target_type = ext
endif
let target_format = ext == '' ? s:save_format() : s:ext_to_fmt(ext)
endif
let target_path = fnamemodify(s:save_as_tmp_puml_path, ':p:r') . '.' . target_type
let target_path = printf("%s.%s", fnamemodify(s:save_as_tmp_puml_path, ':p:r'), target_format)
let content = getline(1,'$')
call mkdir(s:tmp_path, 'p')
call writefile(content, s:save_as_tmp_puml_path)
call mkdir(fnamemodify(save_path, ':p:h'), 'p')
let cmd = [
\ s:save_as_script_path,
\ plantuml_previewer#jar_path(),
\ s:jar_path(),
\ s:save_as_tmp_puml_path,
\ target_type,
\ target_format,
\ target_path,
\ save_path,
\ ]
Expand Down
16 changes: 13 additions & 3 deletions doc/plantuml-previewer.txt
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,10 @@ COMMANDS *plantuml-previewer-interface-commands*
Stop watch buffer

*:PlantumlSave*
:PlantumlSave {filepath} [{format}]
Export uml diagram to file path.
:PlantumlSave [{filepath}] [{format}]
Export uml diagram
If {filepath} is missing, the {filepath} using the same file path as
plant uml, and default extension guessed by |g:plantuml_previewer#save_format|
If {format} is missing, the {format} will be guessed by {filepath}
extension.

Expand All @@ -68,6 +70,9 @@ COMMANDS *plantuml-previewer-interface-commands*
scxml, html, txt, utxt, latex
<
Example: >
:e diagram.puml
:PlantumlSave
:PlantumlSave diagram.png
:PlantumlSave diagram.svg
<
Expand All @@ -77,7 +82,12 @@ VARIABLES *plantuml-previewer-interface-variables*
*g:plantuml_previewer#plantuml_jar_path*
g:plantuml_previewer#plantuml_jar_path
plantuml.jar path
Default: lib/plantuml.jar
Default: "lib/plantuml.jar"

*g:plantuml_previewer#save_format*
g:plantuml_previewer#save_format
default |:PlantumlSave| format
Default: "png"


==============================================================================
Expand Down
2 changes: 1 addition & 1 deletion plugin/plantuml-previewer.vim
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
command! PlantumlOpen call plantuml_previewer#start()
command! PlantumlStop call plantuml_previewer#stop()
command! -nargs=+ -complete=file PlantumlSave call plantuml_previewer#save_as(<f-args>)
command! -nargs=* -complete=file PlantumlSave call plantuml_previewer#save_as(<f-args>)

0 comments on commit 2ac4f98

Please sign in to comment.