Skip to content

Commit 59c77f9

Browse files
naddeoaAnthony Naddeo
authored and
Anthony Naddeo
committed
Added support for converting to Mediawiki syntax.
My job uses Mediawiki based wikis. This allows me to continuing using vim-notes and share my notes when my coworkers need to see them.
1 parent caac537 commit 59c77f9

File tree

2 files changed

+104
-0
lines changed

2 files changed

+104
-0
lines changed

autoload/xolox/notes/mediawiki.vim

+103
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
" Vim auto-load script
2+
" Author: Anthony Naddeo <[email protected]>
3+
" Last Change: December 14, 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 Mediawik in %s.", g:xolox#notes#version, starttime)
28+
return mediawiki . "\n\n"
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 printf("= %s =", text)
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('<source lang="%s">%s</source>', a:block.language, a:block.text)
44+
elseif a:block.type == 'divider'
45+
"TODO is there an equivelant here for mediawiki?
46+
return ''
47+
elseif a:block.type == 'list'
48+
let items = []
49+
if a:block.ordered
50+
let counter = 1
51+
for item in a:block.items
52+
let indent = repeat('#', item.indent + 1)
53+
let text = s:make_urls_explicit(item.text)
54+
if text =~ "DONE"
55+
call add(items, printf("%s ~~%s~~", indent, text))
56+
else
57+
call add(items, printf("%s %s", indent, text))
58+
endif
59+
let counter += 1
60+
endfor
61+
else
62+
for item in a:block.items
63+
let indent = repeat('*', item.indent + 1)
64+
let text = s:make_urls_explicit(item.text)
65+
if text =~ "DONE"
66+
call add(items, printf("%s ~~%s~~", indent, text))
67+
else
68+
call add(items, printf("%s %s", indent, text))
69+
endif
70+
endfor
71+
endif
72+
let counter = 1
73+
return join(items, "\n")
74+
elseif a:block.type == 'block-quote'
75+
let lines = []
76+
for line in a:block.lines
77+
let prefix = repeat('>', line.level)
78+
call add(lines, printf('%s %s', prefix, line.text))
79+
endfor
80+
return join(lines, "\n")
81+
elseif a:block.type == 'paragraph'
82+
let text = s:make_urls_explicit(a:block.text)
83+
if len(text) <= 50 && text =~ ':$'
84+
let text = printf("'''%s'''", text)
85+
endif
86+
return text
87+
else
88+
let msg = "Encountered unsupported block: %s!"
89+
throw printf(msg, string(a:block))
90+
endif
91+
endfunction
92+
93+
function! s:make_urls_explicit(text) " {{{1
94+
" In the vim-notes syntax, URLs are implicitly hyperlinks.
95+
" In Mediawiki syntax they have to be wrapped in [[markers]].
96+
return substitute(a:text, g:xolox#notes#url_pattern, '\= s:url_callback(submatch(0))', 'g')
97+
endfunction
98+
99+
function! s:url_callback(url)
100+
let label = substitute(a:url, '^\w\+:\(//\)\?', '', '')
101+
return printf('[%s %s]', a:url, label)
102+
endfunction
103+

plugin/notes.vim

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ 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 NoteToMediawiki call xolox#notes#mediawiki#view()
4041
command! -bar -nargs=? NoteToHtml call xolox#notes#html#view(<q-args>)
4142

4243
" TODO Generalize this so we have one command + modifiers (like :tab)?

0 commit comments

Comments
 (0)