From 2141fde8b8c68ce8c6136673890acc354dc78f8c Mon Sep 17 00:00:00 2001 From: ysftaha Date: Sat, 18 Dec 2021 09:40:17 -0500 Subject: [PATCH] Addresses issue #621 (#1181) * issue#621: html highlighter * docs for issue#621 * removing windows support for issue#621 Co-authored-by: Tinmarino --- autoload/vimwiki/html.vim | 26 ++++++++++++++++++++++++++ autoload/vimwiki/vars.vim | 2 ++ doc/vimwiki.txt | 36 ++++++++++++++++++++++++++++++++++++ 3 files changed, 64 insertions(+) diff --git a/autoload/vimwiki/html.vim b/autoload/vimwiki/html.vim index b2f7441..4416e14 100644 --- a/autoload/vimwiki/html.vim +++ b/autoload/vimwiki/html.vim @@ -1879,6 +1879,32 @@ function! s:convert_file(path_html, wikifile) abort return '' endif call vimwiki#path#mkdir(path_html) + + if g:vimwiki_global_vars['listing_hl'] > 0 && has("unix") + let i = 0 + while i < len(html_lines) + if html_lines[i] =~ '^
'
+          let type = split(split(split(html_lines[i], 'type=')[1], '>')[0], '\s\+')[0]
+          let attr = split(split(html_lines[i], '
')[0]
+          let start = i + 1
+          let cur = start
+
+          while html_lines[cur] !~ '^<\/pre>'
+            let cur += 1
+          endwhile
+
+          let tmp = ('tmp'. split(system('mktemp -p . --suffix=.' . type, 'silent'), 'tmp')[-1])[:-2]
+          call system('echo ' . shellescape(join(html_lines[start:cur - 1], '\n')) . ' > ' . tmp)
+          call system(g:vimwiki_global_vars['listing_hl_command'] . ' ' . tmp  . ' > ' . tmp . '.html')
+          let html_out = system('cat ' . tmp . '.html')
+          call system('rm ' . tmp . ' ' . tmp . '.html')
+          let i = cur
+          let html_lines = html_lines[0:start - 1] + split(html_out, '\n') + html_lines[cur:]
+        endif
+        let i += 1
+      endwhile
+    endif
+
     call writefile(html_lines, path_html.htmlfile)
     return path_html . htmlfile
   endif
diff --git a/autoload/vimwiki/vars.vim b/autoload/vimwiki/vars.vim
index da374fb..50b4b6e 100644
--- a/autoload/vimwiki/vars.vim
+++ b/autoload/vimwiki/vars.vim
@@ -167,6 +167,8 @@ function! s:get_default_global() abort
         \   }},
         \ 'links_header': {'type': type(''), 'default': 'Generated Links', 'min_length': 1},
         \ 'links_header_level': {'type': type(0), 'default': 1, 'min': 1, 'max': 6},
+        \ 'listing_hl': {'type': type(0), 'default': 0, 'min': 0, 'max': 1},
+        \ 'listing_hl_command': {'type': type(''), 'default': 'pygmentize -f html'},
         \ 'listsyms': {'type': type(''), 'default': ' .oOX', 'min_length': 2},
         \ 'listsym_rejected': {'type': type(''), 'default': '-', 'length': 1},
         \ 'map_prefix': {'type': type(''), 'default': 'w'},
diff --git a/doc/vimwiki.txt b/doc/vimwiki.txt
index 2c12b49..639eb8d 100644
--- a/doc/vimwiki.txt
+++ b/doc/vimwiki.txt
@@ -3572,6 +3572,40 @@ The header level of generated links. Valid values are from 1 to 6.
 The default is 1.
 
 
+------------------------------------------------------------------------------
+*g:vimwiki_listing_hl*
+
+When set to 1, enables syntax highlighting in resulting HTML. Whenever a code
+block is encountered the *g:vimwiki_listing_hl_command* is invoked on the
+code block.
+
+
+Value           Description~
+0               Do not highlight code blocks in HTML.
+1               Highlight code blocks in HTML. see *g:vimwiki_listing_hl_command*
+
+Default: 0
+
+
+------------------------------------------------------------------------------
+*g:vimwiki_listing_hl_command*
+
+A string specifying the command for highlighting code blocks in html. The
+argument is going to be a file with an extension specified by a
+type=extension. For example, a python listing would be written like s: >
+  {{{type=py
+  print("hello world")
+  }}}
+The argument in this case is going to be a `foo.py`. The result of the
+highlighting command is going to be enclosed in pre tags with the `type`
+attribute. >
+  
+  %output of highlight command&
+  
+ +The default is 'pygmentize -f html'. + + ------------------------------------------------------------------------------ *g:vimwiki_tags_header* @@ -3924,6 +3958,7 @@ Contributors and their Github usernames in roughly chronological order: - Vinny Furia (@vinnyfuria) - paperbenni (@paperbenni) - Lily Foster (@lilyinstarlight) + - Youssof Taha (@ysftaha) - Thomas Leyh (@leyhline) ============================================================================== @@ -3936,6 +3971,7 @@ http://code.google.com/p/vimwiki/issues/list. They may be accessible from https://github.com/vimwiki-backup/vimwiki/issues. New:~ + * Issue #621: Feature request: Highlight code listings in HTML * Issue #290: Calendar plugin, do not sign if no wiki * Issue #281: Permit `\|` in tables * PR #1128: VimwikiGoto: Support wiki pages with spaces