Skip to content

Added ability to open html in a new split. #103

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Dec 29, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,14 @@ If for any reason you want to recreate the list of tags you can execute the `:In

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 it in a browser, you can run:

:NoteToHtml

Alternatively, to convert your note to html and display it in a new split in vim, you can run:

:NoteToHtml split

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).

### The `:NoteToMarkdown` command
Expand All @@ -246,6 +254,16 @@ Convert the current note to a [Markdown document] [markdown]. The vim-notes synt

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).

### The `:NoteToMediawiki` command

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:

* 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.

* 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.

* Currently, the `***` divider isn't translated into anything.

## Mappings

The following key mappings are defined inside notes.
Expand Down Expand Up @@ -360,6 +378,8 @@ This software is licensed under the [MIT license] [mit].
[levenshtein]: http://en.wikipedia.org/wiki/Levenshtein_distance
[mapleader]: http://vimdoc.sourceforge.net/htmldoc/map.html#mapleader
[markdown]: http://en.wikipedia.org/wiki/Markdown
[mediawiki]: https://www.mediawiki.org/wiki/MediaWiki
[geshi]: http://www.mediawiki.org/wiki/Extension:SyntaxHighlight_GeSHi
[mit]: http://en.wikipedia.org/wiki/MIT_License
[modeline]: http://vimdoc.sourceforge.net/htmldoc/options.html#modeline
[monaco]: http://en.wikipedia.org/wiki/Monaco_(typeface)
Expand Down
12 changes: 9 additions & 3 deletions autoload/xolox/notes/html.vim
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ if !exists('g:notes_markdown_program')
let g:notes_markdown_program = 'markdown'
endif

function! xolox#notes#html#view() " {{{1
function! xolox#notes#html#view(open_in) " {{{1
" Convert the current note to a web page and show the web page in a browser.
" Requires [Markdown] [markdown] to be installed; you'll get a warning if it
" isn't.
Expand All @@ -32,9 +32,15 @@ function! xolox#notes#html#view() " {{{1
if writefile(split(styled_html, "\n"), filename) != 0
throw printf("Failed to write HTML file! (%s)", filename)
endif

" Open the generated HTML in a web browser.
call xolox#misc#open#url('file://' . filename)
call xolox#misc#timer#stop("notes.vim %s: Rendered HTML preview in %s.", g:xolox#notes#version, starttime)
if a:open_in == "split"
vnew
call setline(1, split(styled_html, "\n"))
else
call xolox#misc#open#url('file://' . filename)
call xolox#misc#timer#stop("notes.vim %s: Rendered HTML preview in %s.", g:xolox#notes#version, starttime)
endif
catch
call xolox#misc#msg#warn("notes.vim %s: %s at %s", g:xolox#notes#version, v:exception, v:throwpoint)
endtry
Expand Down
115 changes: 115 additions & 0 deletions autoload/xolox/notes/mediawiki.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
" Vim auto-load script
" Author: Anthony Naddeo <anthony.naddeo@gmail.com>
" Last Change: December 14, 2014
" URL: https://github.com/naddeoa

function! xolox#notes#mediawiki#view() " {{{1
" Convert the current note to a Mediawiki document and show the converted text.
let note_text = join(getline(1, '$'), "\n")
let mediawiki_text = xolox#notes#mediawiki#convert_note(note_text)
vnew
call setline(1, split(mediawiki_text, "\n"))
setlocal filetype=mediawiki
endfunction

function! xolox#notes#mediawiki#convert_note(note_text) " {{{1
" Convert a note's text to the [Mediawiki text format] [mediawiki]. The syntax
" used by vim-notes has a lot of similarities with Mediawiki, but there are
" some notable differences like the note title and the way code blocks are
" represented. This function takes the text of a note (the first argument)
" and converts it to the Mediawiki format, returning a string.
"
" [mediawiki]: https://www.mediawiki.org/wiki/MediaWiki
let starttime = xolox#misc#timer#start()
let blocks = xolox#notes#parser#parse_note(a:note_text)
call map(blocks, 'xolox#notes#mediawiki#convert_block(v:val)')
let mediawiki = join(blocks, "\n\n")
call xolox#misc#timer#stop("notes.vim %s: Converted note to Mediawik in %s.", g:xolox#notes#version, starttime)
return mediawiki . "\n\n"
endfunction

function! xolox#notes#mediawiki#convert_block(block) " {{{1
" Convert a single block produced by `xolox#misc#notes#parser#parse_note()`
" (the first argument, expected to be a dictionary) to the [Mediawiki text
" format] [mediawiki]. Returns a string.
if a:block.type == 'title'
let text = s:make_urls_explicit(a:block.text)
return ""
elseif a:block.type == 'heading'
let marker = repeat('=', 1 + a:block.level)
let text = s:make_urls_explicit(a:block.text)
return printf("%s %s %s", marker, text, marker)
elseif a:block.type == 'code'
return printf('<syntaxhighlight lang="%s">%s</syntaxhighlight>', a:block.language, a:block.text)
elseif a:block.type == 'divider'
"TODO is there an equivelant here for mediawiki?
return ''
elseif a:block.type == 'list'
let items = []
if a:block.ordered
let counter = 1
for item in a:block.items
let indent = repeat('#', item.indent + 1)
let text = s:make_urls_explicit(item.text)
let text = s:hilight_tasks(text)
if text =~# "DONE"
call add(items, printf("%s <del>%s</del>", indent, text))
else
call add(items, printf("%s %s", indent, text))
endif
let counter += 1
endfor
else
for item in a:block.items
let indent = repeat('*', item.indent + 1)
let text = s:make_urls_explicit(item.text)
let text = s:hilight_tasks(text)
if text =~# "DONE"
call add(items, printf("%s <del>%s</del>", indent, text))
else
call add(items, printf("%s %s", indent, text))
endif
endfor
endif
let counter = 1
return join(items, "\n")
elseif a:block.type == 'block-quote'
let lines = []
for line in a:block.lines
let prefix = repeat('>', line.level)
call add(lines, printf('%s %s', prefix, line.text))
endfor
return join(lines, "\n")
elseif a:block.type == 'paragraph'
let text = s:make_urls_explicit(a:block.text)
if len(text) <= 50 && text =~ ':$'
let text = printf("'''%s'''", text)
endif
return text
else
let msg = "Encountered unsupported block: %s!"
throw printf(msg, string(a:block))
endif
endfunction

function! s:hilight_tasks(text)
" Make case sensitive matches of DONE, XXX, and TODO so that we can color
" them in the output the same way they are colored in vim-notes
let hilighted = a:text
let hilighted = substitute(hilighted, "DONE\\C", '<span style="color:green">DONE</span>', "")
let hilighted = substitute(hilighted, "XXX\\C", '<span style="color:red">XXX</span>', "")
let hilighted = substitute(hilighted, "TODO\\C", '<span style="color:red">TODO</span>', "")
return hilighted
endfunction

function! s:make_urls_explicit(text) " {{{1
" In the vim-notes syntax, URLs are implicitly hyperlinks.
" In Mediawiki syntax they have to be wrapped in [[markers]].
return substitute(a:text, g:xolox#notes#url_pattern, '\= s:url_callback(submatch(0))', 'g')
endfunction

function! s:url_callback(url)
let label = substitute(a:url, '^\w\+:\(//\)\?', '', '')
return printf('[%s %s]', a:url, label)
endfunction

3 changes: 2 additions & 1 deletion plugin/notes.vim
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ command! -bar -bang MostRecentNote call xolox#notes#recent#edit(<q-bang>)
command! -bar -count=1 ShowTaggedNotes call xolox#notes#tags#show_tags(<count>)
command! -bar IndexTaggedNotes call xolox#notes#tags#create_index()
command! -bar NoteToMarkdown call xolox#notes#markdown#view()
command! -bar NoteToHtml call xolox#notes#html#view()
command! -bar NoteToMediawiki call xolox#notes#mediawiki#view()
command! -bar -nargs=? NoteToHtml call xolox#notes#html#view(<q-args>)

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