Skip to content

Commit ed6d6e7

Browse files
committed
Merge pull request #103: Open HTML in split & Mediawiki converter
In this merge commit I'm making a couple of minor changes: - The `---` sequence is now used as a horizontal divider in the Mediawiki syntax converter. - The changes to xolox#notes#html#view() created a temporary file even when the HTML was shown in a Vim split window, now the temporary file is only created when it needs to be passed to a web browser. - I changed some wrong indentation, removed some unused code (the counter variable) and renamed some private variables (s/hilight/highlight/g) and added word boundaries to the TODO/DONE/XXX matching in the file mediawiki.vim. - No trailing empty lines are added when a note is converted to Mediawiki syntax (there were in the pull request, but without context or explanation so I'd rather keep the Markdown, HTML and Mediawiki converters consistent).
2 parents 6f4ad2b + 703508d commit ed6d6e7

File tree

6 files changed

+190
-27
lines changed

6 files changed

+190
-27
lines changed

README.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,13 @@ If for any reason you want to recreate the list of tags you can execute the `:In
230230

231231
### The `:NoteToHtml` command
232232

233-
This command converts the current note to HTML. It works by first converting the current note to [Markdown] [markdown] and then using the `markdown` program to convert that to HTML. It requires an external program to convert Markdown to HTML. By default the program `markdown` is used, but you can change the name of the program using the `g:notes_markdown_program` option.
233+
This command converts the current note to HTML. It works by first converting the current note to [Markdown] [markdown] and then using the `markdown` program to convert that to HTML. It requires an external program to convert Markdown to HTML. By default the program `markdown` is used, but you can change the name of the program using the `g:notes_markdown_program` option. To convert your note to HTML and open the generated web page in a browser, you can run:
234+
235+
:NoteToHtml
236+
237+
Alternatively, to convert your note to HTML and display it in a new split window in Vim, you can run:
238+
239+
:NoteToHtml split
234240

235241
Note that this command can be a bit slow, because the parser for the note taking syntax is written in Vim script (for portability) and has not been optimized for speed (yet).
236242

@@ -246,6 +252,14 @@ Convert the current note to a [Markdown document] [markdown]. The vim-notes synt
246252

247253
Note that this command can be a bit slow, because the parser for the note taking syntax is written in Vim script (for portability) and has not been optimized for speed (yet).
248254

255+
### The `:NoteToMediawiki` command
256+
257+
Convert the current note to a [Mediawiki document] [mediawiki]. This is similar to the `:NoteToMarkdown` command, but it produces wiki text that can be displayed on a Mediawiki site. That being said, the subset of wiki markup that vim-notes actually produces will probably work on other wiki sites. These are the notable transforations:
258+
259+
* The first line of the note is a title, but it isn't used in the Mediawiki syntax. It could have been put into a `= Title =` tag, but it doesn't really make sense in the context of a wiki. It would make the table of contents nest under the title for every document you create.
260+
261+
* Preformatted blocks are output into `<syntaxhighlight lang="..">` tags. This functionality is enabled on Mediawiki through the [SyntaxHighlight GeSHi extention] [geshi]. It is also supported on Wikipedia.
262+
249263
## Mappings
250264

251265
The following key mappings are defined inside notes.
@@ -351,6 +365,7 @@ This software is licensed under the [MIT license] [mit].
351365
[download-notes]: http://peterodding.com/code/vim/downloads/notes.zip
352366
[edit]: http://vimdoc.sourceforge.net/htmldoc/editing.html#:edit
353367
[enc]: http://vimdoc.sourceforge.net/htmldoc/options.html#'encoding'
368+
[geshi]: http://www.mediawiki.org/wiki/Extension:SyntaxHighlight_GeSHi
354369
[gf]: http://vimdoc.sourceforge.net/htmldoc/editing.html#gf
355370
[gfm]: https://help.github.com/articles/github-flavored-markdown/
356371
[github-misc]: http://github.com/xolox/vim-misc
@@ -360,6 +375,7 @@ This software is licensed under the [MIT license] [mit].
360375
[levenshtein]: http://en.wikipedia.org/wiki/Levenshtein_distance
361376
[mapleader]: http://vimdoc.sourceforge.net/htmldoc/map.html#mapleader
362377
[markdown]: http://en.wikipedia.org/wiki/Markdown
378+
[mediawiki]: https://www.mediawiki.org/wiki/MediaWiki
363379
[mit]: http://en.wikipedia.org/wiki/MIT_License
364380
[modeline]: http://vimdoc.sourceforge.net/htmldoc/options.html#modeline
365381
[monaco]: http://en.wikipedia.org/wiki/Monaco_(typeface)

autoload/xolox/notes.vim

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
" Vim auto-load script
22
" Author: Peter Odding <peter@peterodding.com>
3-
" Last Change: November 27, 2014
3+
" Last Change: December 29, 2014
44
" URL: http://peterodding.com/code/vim/notes/
55

66
" Note: This file is encoded in UTF-8 including a byte order mark so
77
" that Vim loads the script using the right encoding transparently.
88

9-
let g:xolox#notes#version = '0.29'
9+
let g:xolox#notes#version = '0.30'
1010
let g:xolox#notes#url_pattern = '\<\(mailto:\|javascript:\|\w\{3,}://\)\(\S*\w\)\+/\?'
1111
let s:scriptdir = expand('<sfile>:p:h')
1212

autoload/xolox/notes/html.vim

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
" Vim auto-load script
22
" Author: Peter Odding <peter@peterodding.com>
3-
" Last Change: June 23, 2013
3+
" Last Change: December 29, 2014
44
" URL: http://peterodding.com/code/vim/notes/
55

66
if !exists('g:notes_markdown_program')
77
let g:notes_markdown_program = 'markdown'
88
endif
99

10-
function! xolox#notes#html#view() " {{{1
10+
function! xolox#notes#html#view(open_in) " {{{1
1111
" Convert the current note to a web page and show the web page in a browser.
1212
" Requires [Markdown] [markdown] to be installed; you'll get a warning if it
1313
" isn't.
@@ -28,12 +28,19 @@ function! xolox#notes#html#view() " {{{1
2828
\ 'date': strftime('%A %B %d, %Y at %H:%M'),
2929
\ 'filename': fnamemodify(filename, ':~'),
3030
\ })
31-
let filename = s:create_temporary_file(note_title)
32-
if writefile(split(styled_html, "\n"), filename) != 0
33-
throw printf("Failed to write HTML file! (%s)", filename)
31+
if a:open_in == "split"
32+
" Open the generated HTML in a :split window.
33+
vnew
34+
call setline(1, split(styled_html, "\n"))
35+
setlocal filetype=html
36+
else
37+
" Open the generated HTML in a web browser.
38+
let filename = s:create_temporary_file(note_title)
39+
if writefile(split(styled_html, "\n"), filename) != 0
40+
throw printf("Failed to write HTML file! (%s)", filename)
41+
endif
42+
call xolox#misc#open#url('file://' . filename)
3443
endif
35-
" Open the generated HTML in a web browser.
36-
call xolox#misc#open#url('file://' . filename)
3744
call xolox#misc#timer#stop("notes.vim %s: Rendered HTML preview in %s.", g:xolox#notes#version, starttime)
3845
catch
3946
call xolox#misc#msg#warn("notes.vim %s: %s at %s", g:xolox#notes#version, v:exception, v:throwpoint)

autoload/xolox/notes/mediawiki.vim

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
" Vim auto-load script
2+
" Author: Anthony Naddeo <anthony.naddeo@gmail.com>
3+
" Last Change: December 29, 2014
4+
" URL: https://github.com/naddeoa
5+
6+
function! xolox#notes#mediawiki#view() " {{{1
7+
" Convert the current note to a Mediawiki document and show the converted text.
8+
let note_text = join(getline(1, '$'), "\n")
9+
let mediawiki_text = xolox#notes#mediawiki#convert_note(note_text)
10+
vnew
11+
call setline(1, split(mediawiki_text, "\n"))
12+
setlocal filetype=mediawiki
13+
endfunction
14+
15+
function! xolox#notes#mediawiki#convert_note(note_text) " {{{1
16+
" Convert a note's text to the [Mediawiki text format] [mediawiki]. The syntax
17+
" used by vim-notes has a lot of similarities with Mediawiki, but there are
18+
" some notable differences like the note title and the way code blocks are
19+
" represented. This function takes the text of a note (the first argument)
20+
" and converts it to the Mediawiki format, returning a string.
21+
"
22+
" [mediawiki]: https://www.mediawiki.org/wiki/MediaWiki
23+
let starttime = xolox#misc#timer#start()
24+
let blocks = xolox#notes#parser#parse_note(a:note_text)
25+
call map(blocks, 'xolox#notes#mediawiki#convert_block(v:val)')
26+
let mediawiki = join(blocks, "\n\n")
27+
call xolox#misc#timer#stop("notes.vim %s: Converted note to Mediawiki syntax in %s.", g:xolox#notes#version, starttime)
28+
return mediawiki
29+
endfunction
30+
31+
function! xolox#notes#mediawiki#convert_block(block) " {{{1
32+
" Convert a single block produced by `xolox#misc#notes#parser#parse_note()`
33+
" (the first argument, expected to be a dictionary) to the [Mediawiki text
34+
" format] [mediawiki]. Returns a string.
35+
if a:block.type == 'title'
36+
let text = s:make_urls_explicit(a:block.text)
37+
return ""
38+
elseif a:block.type == 'heading'
39+
let marker = repeat('=', 1 + a:block.level)
40+
let text = s:make_urls_explicit(a:block.text)
41+
return printf("%s %s %s", marker, text, marker)
42+
elseif a:block.type == 'code'
43+
return printf('<syntaxhighlight lang="%s">%s</syntaxhighlight>', a:block.language, a:block.text)
44+
elseif a:block.type == 'divider'
45+
return '----'
46+
elseif a:block.type == 'list'
47+
let items = []
48+
if a:block.ordered
49+
for item in a:block.items
50+
let indent = repeat('#', item.indent + 1)
51+
let text = s:make_urls_explicit(item.text)
52+
let text = s:highlight_task_markers(text)
53+
if text =~# "DONE"
54+
call add(items, printf("%s <del>%s</del>", indent, text))
55+
else
56+
call add(items, printf("%s %s", indent, text))
57+
endif
58+
endfor
59+
else
60+
for item in a:block.items
61+
let indent = repeat('*', item.indent + 1)
62+
let text = s:make_urls_explicit(item.text)
63+
let text = s:highlight_task_markers(text)
64+
if text =~# "DONE"
65+
call add(items, printf("%s <del>%s</del>", indent, text))
66+
else
67+
call add(items, printf("%s %s", indent, text))
68+
endif
69+
endfor
70+
endif
71+
return join(items, "\n")
72+
elseif a:block.type == 'block-quote'
73+
let lines = []
74+
for line in a:block.lines
75+
let prefix = repeat('>', line.level)
76+
call add(lines, printf('%s %s', prefix, line.text))
77+
endfor
78+
return join(lines, "\n")
79+
elseif a:block.type == 'paragraph'
80+
let text = s:make_urls_explicit(a:block.text)
81+
if len(text) <= 50 && text =~ ':$'
82+
let text = printf("'''%s'''", text)
83+
endif
84+
return text
85+
else
86+
let msg = "Encountered unsupported block: %s!"
87+
throw printf(msg, string(a:block))
88+
endif
89+
endfunction
90+
91+
function! s:highlight_task_markers(text)
92+
" Highlight `TODO`, `DONE` and `XXX` markers with color in the Mediawiki
93+
" output similar to how the markers are highlighted by vim-notes.
94+
let highlighted = a:text
95+
let highlighted = substitute(highlighted, '\C\<XXX\>', '<span style="color:red">XXX</span>', "")
96+
let highlighted = substitute(highlighted, '\C\<TODO\>', '<span style="color:red">TODO</span>', "")
97+
let highlighted = substitute(highlighted, '\C\<DONE\>', '<span style="color:green">DONE</span>', "")
98+
return highlighted
99+
endfunction
100+
101+
function! s:make_urls_explicit(text) " {{{1
102+
" In the vim-notes syntax, URLs are implicitly hyperlinks.
103+
" In Mediawiki syntax they have to be wrapped in [[markers]].
104+
return substitute(a:text, g:xolox#notes#url_pattern, '\= s:url_callback(submatch(0))', 'g')
105+
endfunction
106+
107+
function! s:url_callback(url)
108+
let label = substitute(a:url, '^\w\+:\(//\)\?', '', '')
109+
return printf('[%s %s]', a:url, label)
110+
endfunction

doc/notes.txt

Lines changed: 45 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ Contents ~
3838
11. The |:IndexTaggedNotes| command
3939
12. The |:NoteToHtml| command
4040
13. The |:NoteToMarkdown| command
41+
14. The |:NoteToMediawiki| command
4142
5. Mappings |notes-mappings|
4243
1. Insert mode mappings |notes-insert-mode-mappings|
4344
6. Customizing the syntax highlighting of notes |customizing-syntax-highlighting-of-notes|
@@ -516,8 +517,16 @@ This command converts the current note to HTML. It works by first converting
516517
the current note to Markdown [13] and then using the 'markdown' program to
517518
convert that to HTML. It requires an external program to convert Markdown to
518519
HTML. By default the program 'markdown' is used, but you can change the name of
519-
the program using the |g:notes_markdown_program| option.
520-
520+
the program using the |g:notes_markdown_program| option. To convert your note
521+
to HTML and open the generated web page in a browser, you can run:
522+
>
523+
:NoteToHtml
524+
<
525+
Alternatively, to convert your note to HTML and display it in a new split
526+
window in Vim, you can run:
527+
>
528+
:NoteToHtml split
529+
<
521530
Note that this command can be a bit slow, because the parser for the note
522531
taking syntax is written in Vim script (for portability) and has not been
523532
optimized for speed (yet).
@@ -543,6 +552,24 @@ Note that this command can be a bit slow, because the parser for the note
543552
taking syntax is written in Vim script (for portability) and has not been
544553
optimized for speed (yet).
545554

555+
-------------------------------------------------------------------------------
556+
The *:NoteToMediawiki* command
557+
558+
Convert the current note to a Mediawiki document [14]. This is similar to the
559+
|:NoteToMarkdown| command, but it produces wiki text that can be displayed on a
560+
Mediawiki site. That being said, the subset of wiki markup that vim-notes
561+
actually produces will probably work on other wiki sites. These are the notable
562+
transforations:
563+
564+
- The first line of the note is a title, but it isn't used in the Mediawiki
565+
syntax. It could have been put into a '= Title =' tag, but it doesn't
566+
really make sense in the context of a wiki. It would make the table of
567+
contents nest under the title for every document you create.
568+
569+
- Preformatted blocks are output into '<syntaxhighlight lang="..">' tags.
570+
This functionality is enabled on Mediawiki through the SyntaxHighlight
571+
GeSHi extention [15]. It is also supported on Wikipedia.
572+
546573
===============================================================================
547574
*notes-mappings*
548575
Mappings ~
@@ -630,14 +657,14 @@ Other plug-ins that work well with the notes plug-in ~
630657
*notes-utl.vim*
631658
utl.vim ~
632659

633-
The utl.vim [14] universal text linking plug-in enables links between your
660+
The utl.vim [16] universal text linking plug-in enables links between your
634661
notes, other local files and remote resources like web pages.
635662

636663
-------------------------------------------------------------------------------
637664
*notes-shell.vim*
638665
shell.vim ~
639666

640-
My shell.vim [15] plug-in also enables easy navigation between your notes and
667+
My shell.vim [17] plug-in also enables easy navigation between your notes and
641668
environment like local files and directories, web pages and e-mail addresses by
642669
providing key mappings and commands to e.g. open the file/URL under the text
643670
cursor. This plug-in can also change Vim to full screen which can be really
@@ -647,7 +674,7 @@ nice for large notes.
647674
*notes-voom*
648675
VOoM ~
649676

650-
The VOoM [16] outlining plug-in should work well for notes if you use the
677+
The VOoM [18] outlining plug-in should work well for notes if you use the
651678
Markdown style headers starting with '#', however it has been reported that
652679
this combination may not always work so well in practice (sometimes losing
653680
notes!)
@@ -657,7 +684,7 @@ notes!)
657684
Txtfmt ~
658685

659686
If the text formatting supported by the notes plug-in is not enough for you,
660-
consider trying the Txtfmt [17] (The Vim Highlighter) plug-in. To use the two
687+
consider trying the Txtfmt [19] (The Vim Highlighter) plug-in. To use the two
661688
plug-ins together, create the file 'after/ftplugin/notes.vim' inside your Vim
662689
profile with the following contents:
663690
>
@@ -673,7 +700,7 @@ absolute pathnames that most certainly won't match between e.g. Windows and
673700
Linux or even Windows and Cygwin. The best you can do is keep separate session
674701
scripts for different platforms (and I would certainly consider Cygwin a
675702
separate platform altogether :-). For more information please refer to issue
676-
#85 [18].
703+
#85 [20].
677704

678705
===============================================================================
679706
*notes-contact*
@@ -682,13 +709,13 @@ Contact ~
682709
If you have questions, bug reports, suggestions, etc. the author can be
683710
contacted at peter@peterodding.com. The latest version is available at
684711
http://peterodding.com/code/vim/notes/ and http://github.com/xolox/vim-notes.
685-
If you like the script please vote for it on Vim Online [19].
712+
If you like the script please vote for it on Vim Online [21].
686713

687714
===============================================================================
688715
*notes-license*
689716
License ~
690717

691-
This software is licensed under the MIT license [20]. © 2014 Peter Odding
718+
This software is licensed under the MIT license [22]. © 2014 Peter Odding
692719
<peter@peterodding.com>.
693720

694721
===============================================================================
@@ -708,12 +735,14 @@ References ~
708735
[11] http://github.com/xolox/vim-notes
709736
[12] http://github.com/xolox/vim-misc
710737
[13] http://en.wikipedia.org/wiki/Markdown
711-
[14] http://www.vim.org/scripts/script.php?script_id=293
712-
[15] http://www.vim.org/scripts/script.php?script_id=3123
713-
[16] http://www.vim.org/scripts/script.php?script_id=2657
714-
[17] http://www.vim.org/scripts/script.php?script_id=2208
715-
[18] https://github.com/xolox/vim-session/issues/85
716-
[19] http://www.vim.org/scripts/script.php?script_id=3375
717-
[20] http://en.wikipedia.org/wiki/MIT_License
738+
[14] https://www.mediawiki.org/wiki/MediaWiki
739+
[15] http://www.mediawiki.org/wiki/Extension:SyntaxHighlight_GeSHi
740+
[16] http://www.vim.org/scripts/script.php?script_id=293
741+
[17] http://www.vim.org/scripts/script.php?script_id=3123
742+
[18] http://www.vim.org/scripts/script.php?script_id=2657
743+
[19] http://www.vim.org/scripts/script.php?script_id=2208
744+
[20] https://github.com/xolox/vim-session/issues/85
745+
[21] http://www.vim.org/scripts/script.php?script_id=3375
746+
[22] http://en.wikipedia.org/wiki/MIT_License
718747

719748
vim: ft=help

plugin/notes.vim

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ command! -bar -bang MostRecentNote call xolox#notes#recent#edit(<q-bang>)
3737
command! -bar -count=1 ShowTaggedNotes call xolox#notes#tags#show_tags(<count>)
3838
command! -bar IndexTaggedNotes call xolox#notes#tags#create_index()
3939
command! -bar NoteToMarkdown call xolox#notes#markdown#view()
40-
command! -bar NoteToHtml call xolox#notes#html#view()
40+
command! -bar NoteToMediawiki call xolox#notes#mediawiki#view()
41+
command! -bar -nargs=? NoteToHtml call xolox#notes#html#view(<q-args>)
4142

4243
" TODO Generalize this so we have one command + modifiers (like :tab)?
4344
command! -bar -bang -range NoteFromSelectedText call xolox#notes#from_selection(<q-bang>, 'edit')

0 commit comments

Comments
 (0)