tohtml.vim 10 KB

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