tohtml.vim 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. " Vim plugin for converting a syntax highlighted file to HTML.
  2. " Maintainer: Ben Fritz <fritzophrenic@gmail.com>
  3. " Last Change: 2023 Jan 01
  4. "
  5. " The core of the code is in $VIMRUNTIME/autoload/tohtml.vim and
  6. " $VIMRUNTIME/syntax/2html.vim
  7. "
  8. if exists('g:loaded_2html_plugin')
  9. finish
  10. endif
  11. let g:loaded_2html_plugin = 'vim9.0_v1'
  12. "
  13. " Changelog: {{{
  14. " 9.0_v1 (this version): - Implement g:html_no_doc and g:html_no_modeline
  15. " for diff mode. Add tests.
  16. " (Vim 9.0.1122): NOTE: no version string update for this version!
  17. " - Bugfix for variable name in g:html_no_doc
  18. " (Vim 9.0.0819): NOTE: no version string update for this version!
  19. " - Add options g:html_no_doc, g:html_no_lines,
  20. " and g:html_no_modeline (partially included in Vim
  21. " runtime prior to version string update).
  22. " - Updates for new Vim9 string append style (i.e. use
  23. " ".." instead of ".")
  24. "
  25. " 8.1 updates: {{{
  26. " 8.1_v2 (Vim 8.1.2312): - Fix SourceForge issue #19: fix calculation of tab
  27. " stop position to use in expanding a tab, when that
  28. " tab occurs after a syntax match which in turn
  29. " comes after previously expanded tabs.
  30. " - Set eventignore while splitting a window for the
  31. " destination file to ignore FileType events;
  32. " speeds up processing when the destination file
  33. " already exists and HTML highlight takes too long.
  34. " - Fix SourceForge issue #20: progress bar could not be
  35. " seen when DiffDelete background color matched
  36. " StatusLine background color. Added TOhtmlProgress
  37. " highlight group for manual user override, but
  38. " calculate it to be visible compared to StatusLine
  39. " by default.
  40. " - Fix SourceForge issue #1: Remove workaround for old
  41. " browsers which don't support 'ch' CSS unit, since
  42. " all modern browsers, including IE>=9, support it.
  43. " - Fix SourceForge issue #10: support termguicolors
  44. " - Fix SourceForge issue #21: default to using
  45. " generated content instead of <input> tags for
  46. " uncopyable text, so that text is correctly
  47. " prevented from being copied in chrome. Use
  48. " g:html_use_input_for_pc option to control the
  49. " method used.
  50. " - Switch to HTML5 to allow using vnu as a validator
  51. " in unit test.
  52. " - Fix fallback sizing of <input> tags for browsers
  53. " without "ch" support.
  54. " - Fix cursor on unselectable diff filler text.
  55. " 8.1_v1 (Vim 8.1.0528): - Fix SourceForge issue #6: Don't generate empty
  56. " script tag.
  57. " - Fix SourceForge issue #5: javascript should
  58. " declare variables with "var".
  59. " - Fix SourceForge issue #13: errors thrown sourcing
  60. " 2html.vim directly when plugins not loaded.
  61. " - Fix SourceForge issue #16: support 'vartabstop'.
  62. "}}}
  63. "
  64. " 7.4 updates: {{{
  65. " 7.4_v2 (Vim 7.4.0899): Fix error raised when converting a diff containing
  66. " an empty buffer. Jan Stocker: allow g:html_font to
  67. " take a list so it is easier to specfiy fallback
  68. " fonts in the generated CSS.
  69. " 7.4_v1 (Vim 7.4.0000): Fix modeline mangling for new "Vim:" format, and
  70. " also for version-specific modelines like "vim>703:".
  71. "}}}
  72. "
  73. " 7.3 updates: {{{
  74. " 7.3_v14 (Vim 7.3.1246): Allow suppressing line number anchors using
  75. " g:html_line_ids=0. Allow customizing
  76. " important IDs (like line IDs and fold IDs) using
  77. " g:html_id_expr evaluated when the buffer conversion
  78. " is started.
  79. " 7.3_v13 (Vim 7.3.1088): Keep foldmethod at manual in the generated file and
  80. " insert modeline to set it to manual.
  81. " Fix bug: diff mode with 2 unsaved buffers creates a
  82. " duplicate of one buffer instead of including both.
  83. " Add anchors to each line so you can put '#L123'
  84. " or '#123' at the end of the URL to jump to line 123
  85. " (idea by Andy Spencer). Add javascript to open folds
  86. " to show the anchor being jumped to if it is hidden.
  87. " Fix XML validation error: &nsbp; not part of XML.
  88. " Allow TOhtml to chain together with other commands
  89. " using |.
  90. " 7.3_v12 (Vim 7.3.0616): Fix modeline mangling to also work for when multiple
  91. " highlight groups make up the start-of-modeline text.
  92. " Improve render time of page with uncopyable regions
  93. " by not using one-input-per-char. Change name of
  94. " uncopyable option from html_unselectable to
  95. " html_prevent_copy. Added html_no_invalid option and
  96. " default to inserting invalid markup for uncopyable
  97. " regions to prevent MS Word from pasting undeletable
  98. " <input> elements. Fix 'cpo' handling (Thilo Six).
  99. " 7.3_v12b1: Add html_unselectable option. Rework logic to
  100. " eliminate post-processing substitute commands in
  101. " favor of doing the work up front. Remove unnecessary
  102. " special treatment of 'LineNr' highlight group. Minor
  103. " speed improvements. Fix modeline mangling in
  104. " generated output so it works for text in the first
  105. " column. Fix missing line number and fold column in
  106. " diff filler lines. Fix that some fonts have a 1px
  107. " gap (using a dirty hack, improvements welcome). Add
  108. " "colorscheme" meta tag. Does NOT include support for
  109. " the new default foldtext added in v11, as the patch
  110. " adding it has not yet been included in Vim.
  111. " 7.3_v11 ( unreleased ): Support new default foldtext from patch by Christian
  112. " Brabandt in
  113. " http://groups.google.com/d/topic/vim_dev/B6FSGfq9VoI/discussion.
  114. " This patch has not yet been included in Vim, thus
  115. " these changes are removed in the next version.
  116. " 7.3_v10 (Vim 7.3.0227): Fix error E684 when converting a range wholly inside
  117. " multiple nested folds with dynamic folding on.
  118. " Also fix problem with foldtext in this situation.
  119. " 7.3_v9 (Vim 7.3.0170): Add html_pre_wrap option active with html_use_css
  120. " and without html_no_pre, default value same as
  121. " 'wrap' option, (Andy Spencer). Don't use
  122. " 'fileencoding' for converted document encoding if
  123. " 'buftype' indicates a special buffer which isn't
  124. " written.
  125. " 7.3_v8 (Vim 7.3.0100): Add html_expand_tabs option to allow leaving tab
  126. " characters in generated output (Andy Spencer).
  127. " Escape text that looks like a modeline so Vim
  128. " doesn't use anything in the converted HTML as a
  129. " modeline. Bugfixes: Fix folding when a fold starts
  130. " before the conversion range. Remove fold column when
  131. " there are no folds.
  132. " 7.3_v7 (Vim 7-3-0063): see betas released on vim_dev below:
  133. " 7.3_v7b3: Fixed bug, convert Unicode to UTF-8 all the way.
  134. " 7.3_v7b2: Remove automatic detection of encodings that are not
  135. " supported by all major browsers according to
  136. " http://wiki.whatwg.org/wiki/Web_Encodings and
  137. " convert to UTF-8 for all Unicode encodings. Make
  138. " HTML encoding to Vim encoding detection be
  139. " case-insensitive for built-in pairs.
  140. " 7.3_v7b1: Remove use of setwinvar() function which cannot be
  141. " called in restricted mode (Andy Spencer). Use
  142. " 'fencoding' instead of 'encoding' to determine by
  143. " charset, and make sure the 'fenc' of the generated
  144. " file matches its indicated charset. Add charsets for
  145. " all of Vim's natively supported encodings.
  146. " 7.3_v6 (Vim 7.3.0000): Really fix bug with 'nowrapscan', 'magic' and other
  147. " user settings interfering with diff mode generation,
  148. " trailing whitespace (e.g. line number column) when
  149. " using html_no_pre, and bugs when using
  150. " html_hover_unfold.
  151. " 7.3_v5 ( unreleased ): Fix bug with 'nowrapscan' and also with out-of-sync
  152. " folds in diff mode when first line was folded.
  153. " 7.3_v4 (Vim 7.3.0000): Bugfixes, especially for xhtml markup, and diff mode
  154. " 7.3_v3 (Vim 7.3.0000): Refactor option handling and make html_use_css
  155. " default to true when not set to anything. Use strict
  156. " doctypes where possible. Rename use_xhtml option to
  157. " html_use_xhtml for consistency. Use .xhtml extension
  158. " when using this option. Add meta tag for settings.
  159. " 7.3_v2 (Vim 7.3.0000): Fix syntax highlighting in diff mode to use both the
  160. " diff colors and the normal syntax colors
  161. " 7.3_v1 (Vim 7.3.0000): Add conceal support and meta tags in output
  162. "}}}
  163. "}}}
  164. " TODO: {{{
  165. " * Check the issue tracker:
  166. " https://sourceforge.net/p/vim-tohtml/issues/search/?q=%21status%3Aclosed
  167. " * Options for generating the CSS in external style sheets. New :TOcss
  168. " command to convert the current color scheme into a (mostly) generic CSS
  169. " stylesheet which can be re-used. Alternate stylesheet support? Good start
  170. " by Erik Falor
  171. " ( https://groups.google.com/d/topic/vim_use/7XTmC4D22dU/discussion ).
  172. " * Add optional argument to :TOhtml command to specify mode (gui, cterm,
  173. " term) to use for the styling. Suggestion by "nacitar".
  174. " * Add way to override or specify which RGB colors map to the color numbers
  175. " in cterm. Get better defaults than just guessing? Suggestion by "nacitar".
  176. " * Disable filetype detection until after all processing is done.
  177. " * Add option for not generating the hyperlink on stuff that looks like a
  178. " URL? Or just color the link to fit with the colorscheme (and only special
  179. " when hovering)?
  180. " * Bug: Opera does not allow printing more than one page if uncopyable
  181. " regions is turned on. Possible solution: Add normal text line numbers with
  182. " display:none, set to display:inline for print style sheets, and hide
  183. " <input> elements for print, to allow Opera printing multiple pages (and
  184. " other uncopyable areas?). May need to make the new text invisible to IE
  185. " with conditional comments to prevent copying it, IE for some reason likes
  186. " to copy hidden text. Other browsers too?
  187. " * Bug: still a 1px gap throughout the fold column when html_prevent_copy is
  188. " "fn" in some browsers. Specifically, in Chromium on Ubuntu (but not Chrome
  189. " on Windows). Perhaps it is font related?
  190. " * Bug: still some gaps in the fold column when html_prevent_copy contains
  191. " 'd' and showing the whole diff (observed in multiple browsers). Only gaps
  192. " on diff lines though.
  193. " * Undercurl support via CSS3, with fallback to dotted or something:
  194. " https://groups.google.com/d/topic/vim_use/BzXA6He1pHg/discussion
  195. " * Redo updates for modified default foldtext (v11) when/if the patch is
  196. " accepted to modify it.
  197. " * Test case +diff_one_file-dynamic_folds+expand_tabs-hover_unfold
  198. " +ignore_conceal-ignore_folding+no_foldcolumn+no_pre+no_progress
  199. " +number_lines-pre_wrap-use_css+use_xhtml+whole_filler.xhtml
  200. " does not show the whole diff filler as it is supposed to?
  201. " * Bug: when 'isprint' is wrong for the current encoding, will generate
  202. " invalid content. Can/should anything be done about this? Maybe a separate
  203. " plugin to correct 'isprint' based on encoding?
  204. " * Check to see if the windows-125\d encodings actually work in Unix without
  205. " the 8bit- prefix. Add prefix to autoload dictionaries for Unix if not.
  206. " * Font auto-detection similar to
  207. " http://www.vim.org/scripts/script.php?script_id=2384 but for a variety of
  208. " platforms.
  209. " * Pull in code from http://www.vim.org/scripts/script.php?script_id=3113 :
  210. " - listchars support
  211. " - full-line background highlight
  212. " - other?
  213. " * Make it so deleted lines in a diff don't create side-scrolling (get it
  214. " free with full-line background highlight above).
  215. " * Restore open/closed folds and cursor position after processing each file
  216. " with option not to restore for speed increase.
  217. " * Add extra meta info (generation time, etc.)?
  218. " * Tidy up so we can use strict doctype in even more situations
  219. " * Implementation detail: add threshold for writing the lines to the html
  220. " buffer before we're done (5000 or so lines should do it)
  221. " * TODO comments for code cleanup scattered throughout
  222. "}}}
  223. " Define the :TOhtml command when:
  224. " - 'compatible' is not set
  225. " - this plugin or user override was not already loaded
  226. " - user commands are available. {{{
  227. if !&cp && !exists(":TOhtml") && has("user_commands")
  228. command -range=% -bar TOhtml :call tohtml#Convert2HTML(<line1>, <line2>)
  229. endif "}}}
  230. " Make sure any patches will probably use consistent indent
  231. " vim: ts=8 sw=2 sts=2 noet fdm=marker