Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Note to HTML converter based on parser & Markdown converter

  • Loading branch information...
commit ae7c0191ef175e81870627941f77216ba818c375 1 parent a2d0e25
@xolox authored
View
6 autoload/xolox/notes.vim
@@ -1,6 +1,6 @@
" Vim auto-load script
" Author: Peter Odding <peter@peterodding.com>
-" Last Change: May 25, 2013
+" Last Change: June 22, 2013
" URL: http://peterodding.com/code/vim/notes/
" Note: This file is encoded in UTF-8 including a byte order mark so
@@ -60,6 +60,10 @@ function! xolox#notes#init() " {{{1
if !exists('g:notes_recentindex')
let g:notes_recentindex = xolox#misc#path#merge(localdir, 'recent.txt')
endif
+ " Define the default location of the template for HTML conversion.
+ if !exists('g:notes_html_template')
+ let g:notes_html_template = xolox#misc#path#merge(localdir, 'template.html')
+ endif
" Define the default action when a note's filename and title are out of sync.
if !exists('g:notes_title_sync')
" Valid values are "no", "change_title", "rename_file" and "prompt".
View
56 autoload/xolox/notes/html.vim
@@ -0,0 +1,56 @@
+" Vim auto-load script
+" Author: Peter Odding <peter@peterodding.com>
+" Last Change: June 23, 2013
+" URL: http://peterodding.com/code/vim/notes/
+
+if !exists('g:notes_markdown_program')
+ let g:notes_markdown_program = 'markdown'
+endif
+
+function! xolox#notes#html#view()
+ " 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.
+ "
+ " [markdown]: http://en.wikipedia.org/wiki/Markdown
+ try
+ " Convert the note's text to HTML using Markdown.
+ let note_text = join(getline(1, '$'), "\n")
+ let raw_html = xolox#notes#html#convert_note(note_text)
+ let styled_html = xolox#notes#html#apply_template(raw_html)
+ " Save the generated HTML to a temporary file.
+ let filename = tempname() . '.html'
+ 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)
+ catch
+ call xolox#misc#msg#warn("notes.vim %s: %s", g:xolox#notes#version, v:exception)
+ endtry
+endfunction
+
+function! xolox#notes#html#convert_note(note_text)
+ " Convert a note's text to a web page (HTML) using the [Markdown text
+ " format] [markdown] as an intermediate format. This function takes the text
+ " of a note (the first argument) and converts it to HTML, returning a
+ " string.
+ if !executable(g:notes_markdown_program)
+ throw "HTML conversion requires the `markdown' program! On Debian/Ubuntu you can install it by executing `sudo apt-get install markdown'."
+ endif
+ let markdown = xolox#notes#markdown#convert_note(a:note_text)
+ let result = xolox#misc#os#exec({'command': g:notes_markdown_program, 'stdin': markdown})
+ return join(result['stdout'], "\n")
+endfunction
+
+function! xolox#notes#html#apply_template(html)
+ " The vim-notes plug-in contains a web page template that's used to provide
+ " a bit of styling when a note is converted to a web page and presented to
+ " the user. This function takes the original HTML produced by [Markdown]
+ " [markdown] (the first argument) and wraps it in the configured template,
+ " returning the final HTML as a string.
+ let filename = expand(g:notes_html_template)
+ call xolox#misc#msg#debug("notes.vim %s: Reading web page template from %s ..", g:xolox#notes#version, filename)
+ let template = join(readfile(filename), "\n")
+ return printf(template, a:html)
+endfunction
View
18 misc/notes/template.html
@@ -0,0 +1,18 @@
+<!doctype html>
+<html>
+ <head>
+ <meta charset="utf-8">
+ <style type="text/css">
+ body {
+ margin: 1em 25%% 10em 1em;
+ font-family: sans-serif;
+ }
+ h1 {
+ text-align: center;
+ }
+ </style>
+ </head>
+ <body>
+ %s
+ </body>
+</html>
View
3  plugin/notes.vim
@@ -1,6 +1,6 @@
" Vim plug-in
" Author: Peter Odding <peter@peterodding.com>
-" Last Change: May 16, 2013
+" Last Change: June 22, 2013
" URL: http://peterodding.com/code/vim/notes/
" Support for automatic update using the GLVS plug-in.
@@ -23,6 +23,7 @@ command! -bar -bang -nargs=? RecentNotes call xolox#notes#recent#show(<q-bang>,
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 NoteToHtml call xolox#notes#html#view()
" TODO Generalize this so we have one command + modifiers (like :tab)?
command! -bar -bang -range NoteFromSelectedText call xolox#notes#from_selection(<q-bang>, 'edit')
Please sign in to comment.
Something went wrong with that request. Please try again.