tohtml.vim 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931
  1. " Vim autoload file for the tohtml plugin.
  2. " Maintainer: Ben Fritz <fritzophrenic@gmail.com>
  3. " Last Change: 2019 Aug 16
  4. "
  5. " Additional contributors:
  6. "
  7. " Original by Bram Moolenaar <Bram@vim.org>
  8. " Diff2HTML() added by Christian Brabandt <cb@256bit.org>
  9. "
  10. " See Mercurial change logs for more!
  11. " this file uses line continuations
  12. let s:cpo_sav = &cpo
  13. set cpo&vim
  14. " Automatically find charsets from all encodings supported natively by Vim. With
  15. " the 8bit- and 2byte- prefixes, Vim can actually support more encodings than
  16. " this. Let the user specify these however since they won't be supported on
  17. " every system.
  18. "
  19. " Note, not all of Vim's supported encodings have a charset to use.
  20. "
  21. " Names in this list are from:
  22. " http://www.iana.org/assignments/character-sets
  23. " g:tohtml#encoding_to_charset: {{{
  24. let g:tohtml#encoding_to_charset = {
  25. \ 'latin1' : 'ISO-8859-1',
  26. \ 'iso-8859-2' : 'ISO-8859-2',
  27. \ 'iso-8859-3' : 'ISO-8859-3',
  28. \ 'iso-8859-4' : 'ISO-8859-4',
  29. \ 'iso-8859-5' : 'ISO-8859-5',
  30. \ 'iso-8859-6' : 'ISO-8859-6',
  31. \ 'iso-8859-7' : 'ISO-8859-7',
  32. \ 'iso-8859-8' : 'ISO-8859-8',
  33. \ 'iso-8859-9' : 'ISO-8859-9',
  34. \ 'iso-8859-10' : '',
  35. \ 'iso-8859-13' : 'ISO-8859-13',
  36. \ 'iso-8859-14' : '',
  37. \ 'iso-8859-15' : 'ISO-8859-15',
  38. \ 'koi8-r' : 'KOI8-R',
  39. \ 'koi8-u' : 'KOI8-U',
  40. \ 'macroman' : 'macintosh',
  41. \ 'cp437' : '',
  42. \ 'cp775' : '',
  43. \ 'cp850' : '',
  44. \ 'cp852' : '',
  45. \ 'cp855' : '',
  46. \ 'cp857' : '',
  47. \ 'cp860' : '',
  48. \ 'cp861' : '',
  49. \ 'cp862' : '',
  50. \ 'cp863' : '',
  51. \ 'cp865' : '',
  52. \ 'cp866' : 'IBM866',
  53. \ 'cp869' : '',
  54. \ 'cp874' : '',
  55. \ 'cp1250' : 'windows-1250',
  56. \ 'cp1251' : 'windows-1251',
  57. \ 'cp1253' : 'windows-1253',
  58. \ 'cp1254' : 'windows-1254',
  59. \ 'cp1255' : 'windows-1255',
  60. \ 'cp1256' : 'windows-1256',
  61. \ 'cp1257' : 'windows-1257',
  62. \ 'cp1258' : 'windows-1258',
  63. \ 'euc-jp' : 'EUC-JP',
  64. \ 'sjis' : 'Shift_JIS',
  65. \ 'cp932' : 'Shift_JIS',
  66. \ 'cp949' : '',
  67. \ 'euc-kr' : 'EUC-KR',
  68. \ 'cp936' : 'GBK',
  69. \ 'euc-cn' : 'GB2312',
  70. \ 'big5' : 'Big5',
  71. \ 'cp950' : 'Big5',
  72. \ 'utf-8' : 'UTF-8',
  73. \ 'ucs-2' : 'UTF-8',
  74. \ 'ucs-2le' : 'UTF-8',
  75. \ 'utf-16' : 'UTF-8',
  76. \ 'utf-16le' : 'UTF-8',
  77. \ 'ucs-4' : 'UTF-8',
  78. \ 'ucs-4le' : 'UTF-8',
  79. \ }
  80. lockvar g:tohtml#encoding_to_charset
  81. " Notes:
  82. " 1. All UCS/UTF are converted to UTF-8 because it is much better supported
  83. " 2. Any blank spaces are there because Vim supports it but at least one major
  84. " web browser does not according to http://wiki.whatwg.org/wiki/Web_Encodings.
  85. " }}}
  86. " Only automatically find encodings supported natively by Vim, let the user
  87. " specify the encoding if it's not natively supported. This function is only
  88. " used when the user specifies the charset, they better know what they are
  89. " doing!
  90. "
  91. " Names in this list are from:
  92. " http://www.iana.org/assignments/character-sets
  93. " g:tohtml#charset_to_encoding: {{{
  94. let g:tohtml#charset_to_encoding = {
  95. \ 'iso_8859-1:1987' : 'latin1',
  96. \ 'iso-ir-100' : 'latin1',
  97. \ 'iso_8859-1' : 'latin1',
  98. \ 'iso-8859-1' : 'latin1',
  99. \ 'latin1' : 'latin1',
  100. \ 'l1' : 'latin1',
  101. \ 'ibm819' : 'latin1',
  102. \ 'cp819' : 'latin1',
  103. \ 'csisolatin1' : 'latin1',
  104. \ 'iso_8859-2:1987' : 'iso-8859-2',
  105. \ 'iso-ir-101' : 'iso-8859-2',
  106. \ 'iso_8859-2' : 'iso-8859-2',
  107. \ 'iso-8859-2' : 'iso-8859-2',
  108. \ 'latin2' : 'iso-8859-2',
  109. \ 'l2' : 'iso-8859-2',
  110. \ 'csisolatin2' : 'iso-8859-2',
  111. \ 'iso_8859-3:1988' : 'iso-8859-3',
  112. \ 'iso-ir-109' : 'iso-8859-3',
  113. \ 'iso_8859-3' : 'iso-8859-3',
  114. \ 'iso-8859-3' : 'iso-8859-3',
  115. \ 'latin3' : 'iso-8859-3',
  116. \ 'l3' : 'iso-8859-3',
  117. \ 'csisolatin3' : 'iso-8859-3',
  118. \ 'iso_8859-4:1988' : 'iso-8859-4',
  119. \ 'iso-ir-110' : 'iso-8859-4',
  120. \ 'iso_8859-4' : 'iso-8859-4',
  121. \ 'iso-8859-4' : 'iso-8859-4',
  122. \ 'latin4' : 'iso-8859-4',
  123. \ 'l4' : 'iso-8859-4',
  124. \ 'csisolatin4' : 'iso-8859-4',
  125. \ 'iso_8859-5:1988' : 'iso-8859-5',
  126. \ 'iso-ir-144' : 'iso-8859-5',
  127. \ 'iso_8859-5' : 'iso-8859-5',
  128. \ 'iso-8859-5' : 'iso-8859-5',
  129. \ 'cyrillic' : 'iso-8859-5',
  130. \ 'csisolatincyrillic' : 'iso-8859-5',
  131. \ 'iso_8859-6:1987' : 'iso-8859-6',
  132. \ 'iso-ir-127' : 'iso-8859-6',
  133. \ 'iso_8859-6' : 'iso-8859-6',
  134. \ 'iso-8859-6' : 'iso-8859-6',
  135. \ 'ecma-114' : 'iso-8859-6',
  136. \ 'asmo-708' : 'iso-8859-6',
  137. \ 'arabic' : 'iso-8859-6',
  138. \ 'csisolatinarabic' : 'iso-8859-6',
  139. \ 'iso_8859-7:1987' : 'iso-8859-7',
  140. \ 'iso-ir-126' : 'iso-8859-7',
  141. \ 'iso_8859-7' : 'iso-8859-7',
  142. \ 'iso-8859-7' : 'iso-8859-7',
  143. \ 'elot_928' : 'iso-8859-7',
  144. \ 'ecma-118' : 'iso-8859-7',
  145. \ 'greek' : 'iso-8859-7',
  146. \ 'greek8' : 'iso-8859-7',
  147. \ 'csisolatingreek' : 'iso-8859-7',
  148. \ 'iso_8859-8:1988' : 'iso-8859-8',
  149. \ 'iso-ir-138' : 'iso-8859-8',
  150. \ 'iso_8859-8' : 'iso-8859-8',
  151. \ 'iso-8859-8' : 'iso-8859-8',
  152. \ 'hebrew' : 'iso-8859-8',
  153. \ 'csisolatinhebrew' : 'iso-8859-8',
  154. \ 'iso_8859-9:1989' : 'iso-8859-9',
  155. \ 'iso-ir-148' : 'iso-8859-9',
  156. \ 'iso_8859-9' : 'iso-8859-9',
  157. \ 'iso-8859-9' : 'iso-8859-9',
  158. \ 'latin5' : 'iso-8859-9',
  159. \ 'l5' : 'iso-8859-9',
  160. \ 'csisolatin5' : 'iso-8859-9',
  161. \ 'iso-8859-10' : 'iso-8859-10',
  162. \ 'iso-ir-157' : 'iso-8859-10',
  163. \ 'l6' : 'iso-8859-10',
  164. \ 'iso_8859-10:1992' : 'iso-8859-10',
  165. \ 'csisolatin6' : 'iso-8859-10',
  166. \ 'latin6' : 'iso-8859-10',
  167. \ 'iso-8859-13' : 'iso-8859-13',
  168. \ 'iso-8859-14' : 'iso-8859-14',
  169. \ 'iso-ir-199' : 'iso-8859-14',
  170. \ 'iso_8859-14:1998' : 'iso-8859-14',
  171. \ 'iso_8859-14' : 'iso-8859-14',
  172. \ 'latin8' : 'iso-8859-14',
  173. \ 'iso-celtic' : 'iso-8859-14',
  174. \ 'l8' : 'iso-8859-14',
  175. \ 'iso-8859-15' : 'iso-8859-15',
  176. \ 'iso_8859-15' : 'iso-8859-15',
  177. \ 'latin-9' : 'iso-8859-15',
  178. \ 'koi8-r' : 'koi8-r',
  179. \ 'cskoi8r' : 'koi8-r',
  180. \ 'koi8-u' : 'koi8-u',
  181. \ 'macintosh' : 'macroman',
  182. \ 'mac' : 'macroman',
  183. \ 'csmacintosh' : 'macroman',
  184. \ 'ibm437' : 'cp437',
  185. \ 'cp437' : 'cp437',
  186. \ '437' : 'cp437',
  187. \ 'cspc8codepage437' : 'cp437',
  188. \ 'ibm775' : 'cp775',
  189. \ 'cp775' : 'cp775',
  190. \ 'cspc775baltic' : 'cp775',
  191. \ 'ibm850' : 'cp850',
  192. \ 'cp850' : 'cp850',
  193. \ '850' : 'cp850',
  194. \ 'cspc850multilingual' : 'cp850',
  195. \ 'ibm852' : 'cp852',
  196. \ 'cp852' : 'cp852',
  197. \ '852' : 'cp852',
  198. \ 'cspcp852' : 'cp852',
  199. \ 'ibm855' : 'cp855',
  200. \ 'cp855' : 'cp855',
  201. \ '855' : 'cp855',
  202. \ 'csibm855' : 'cp855',
  203. \ 'ibm857' : 'cp857',
  204. \ 'cp857' : 'cp857',
  205. \ '857' : 'cp857',
  206. \ 'csibm857' : 'cp857',
  207. \ 'ibm860' : 'cp860',
  208. \ 'cp860' : 'cp860',
  209. \ '860' : 'cp860',
  210. \ 'csibm860' : 'cp860',
  211. \ 'ibm861' : 'cp861',
  212. \ 'cp861' : 'cp861',
  213. \ '861' : 'cp861',
  214. \ 'cp-is' : 'cp861',
  215. \ 'csibm861' : 'cp861',
  216. \ 'ibm862' : 'cp862',
  217. \ 'cp862' : 'cp862',
  218. \ '862' : 'cp862',
  219. \ 'cspc862latinhebrew' : 'cp862',
  220. \ 'ibm863' : 'cp863',
  221. \ 'cp863' : 'cp863',
  222. \ '863' : 'cp863',
  223. \ 'csibm863' : 'cp863',
  224. \ 'ibm865' : 'cp865',
  225. \ 'cp865' : 'cp865',
  226. \ '865' : 'cp865',
  227. \ 'csibm865' : 'cp865',
  228. \ 'ibm866' : 'cp866',
  229. \ 'cp866' : 'cp866',
  230. \ '866' : 'cp866',
  231. \ 'csibm866' : 'cp866',
  232. \ 'ibm869' : 'cp869',
  233. \ 'cp869' : 'cp869',
  234. \ '869' : 'cp869',
  235. \ 'cp-gr' : 'cp869',
  236. \ 'csibm869' : 'cp869',
  237. \ 'windows-1250' : 'cp1250',
  238. \ 'windows-1251' : 'cp1251',
  239. \ 'windows-1253' : 'cp1253',
  240. \ 'windows-1254' : 'cp1254',
  241. \ 'windows-1255' : 'cp1255',
  242. \ 'windows-1256' : 'cp1256',
  243. \ 'windows-1257' : 'cp1257',
  244. \ 'windows-1258' : 'cp1258',
  245. \ 'extended_unix_code_packed_format_for_japanese' : 'euc-jp',
  246. \ 'cseucpkdfmtjapanese' : 'euc-jp',
  247. \ 'euc-jp' : 'euc-jp',
  248. \ 'shift_jis' : 'sjis',
  249. \ 'ms_kanji' : 'sjis',
  250. \ 'sjis' : 'sjis',
  251. \ 'csshiftjis' : 'sjis',
  252. \ 'ibm-thai' : 'cp874',
  253. \ 'csibmthai' : 'cp874',
  254. \ 'ks_c_5601-1987' : 'cp949',
  255. \ 'iso-ir-149' : 'cp949',
  256. \ 'ks_c_5601-1989' : 'cp949',
  257. \ 'ksc_5601' : 'cp949',
  258. \ 'korean' : 'cp949',
  259. \ 'csksc56011987' : 'cp949',
  260. \ 'euc-kr' : 'euc-kr',
  261. \ 'cseuckr' : 'euc-kr',
  262. \ 'gbk' : 'cp936',
  263. \ 'cp936' : 'cp936',
  264. \ 'ms936' : 'cp936',
  265. \ 'windows-936' : 'cp936',
  266. \ 'gb_2312-80' : 'euc-cn',
  267. \ 'iso-ir-58' : 'euc-cn',
  268. \ 'chinese' : 'euc-cn',
  269. \ 'csiso58gb231280' : 'euc-cn',
  270. \ 'big5' : 'big5',
  271. \ 'csbig5' : 'big5',
  272. \ 'utf-8' : 'utf-8',
  273. \ 'iso-10646-ucs-2' : 'ucs-2',
  274. \ 'csunicode' : 'ucs-2',
  275. \ 'utf-16' : 'utf-16',
  276. \ 'utf-16be' : 'utf-16',
  277. \ 'utf-16le' : 'utf-16le',
  278. \ 'utf-32' : 'ucs-4',
  279. \ 'utf-32be' : 'ucs-4',
  280. \ 'utf-32le' : 'ucs-4le',
  281. \ 'iso-10646-ucs-4' : 'ucs-4',
  282. \ 'csucs4' : 'ucs-4'
  283. \ }
  284. lockvar g:tohtml#charset_to_encoding
  285. "}}}
  286. func! tohtml#Convert2HTML(line1, line2) "{{{
  287. let s:settings = tohtml#GetUserSettings()
  288. if !&diff || s:settings.diff_one_file "{{{
  289. if a:line2 >= a:line1
  290. let g:html_start_line = a:line1
  291. let g:html_end_line = a:line2
  292. else
  293. let g:html_start_line = a:line2
  294. let g:html_end_line = a:line1
  295. endif
  296. runtime syntax/2html.vim "}}}
  297. else "{{{
  298. let win_list = []
  299. let buf_list = []
  300. windo if &diff | call add(win_list, winbufnr(0)) | endif
  301. let s:settings.whole_filler = 1
  302. let g:html_diff_win_num = 0
  303. for window in win_list
  304. " switch to the next buffer to convert
  305. exe ":" . bufwinnr(window) . "wincmd w"
  306. " figure out whether current charset and encoding will work, if not
  307. " default to UTF-8
  308. if !exists('g:html_use_encoding') &&
  309. \ (((&l:fileencoding=='' || (&l:buftype!='' && &l:buftype!=?'help'))
  310. \ && &encoding!=?s:settings.vim_encoding)
  311. \ || &l:fileencoding!='' && &l:fileencoding!=?s:settings.vim_encoding)
  312. echohl WarningMsg
  313. echomsg "TOhtml: mismatched file encodings in Diff buffers, using UTF-8"
  314. echohl None
  315. let s:settings.vim_encoding = 'utf-8'
  316. let s:settings.encoding = 'UTF-8'
  317. endif
  318. " set up for diff-mode conversion
  319. let g:html_start_line = 1
  320. let g:html_end_line = line('$')
  321. let g:html_diff_win_num += 1
  322. " convert this file
  323. runtime syntax/2html.vim
  324. " remember the HTML buffer for later combination
  325. call add(buf_list, bufnr('%'))
  326. endfor
  327. unlet g:html_diff_win_num
  328. call tohtml#Diff2HTML(win_list, buf_list)
  329. endif "}}}
  330. unlet g:html_start_line
  331. unlet g:html_end_line
  332. unlet s:settings
  333. endfunc "}}}
  334. func! tohtml#Diff2HTML(win_list, buf_list) "{{{
  335. let xml_line = ""
  336. let tag_close = '>'
  337. let s:old_paste = &paste
  338. set paste
  339. let s:old_magic = &magic
  340. set magic
  341. if s:settings.use_xhtml
  342. if s:settings.encoding != ""
  343. let xml_line = "<?xml version=\"1.0\" encoding=\"" . s:settings.encoding . "\"?>"
  344. else
  345. let xml_line = "<?xml version=\"1.0\"?>"
  346. endif
  347. let tag_close = ' />'
  348. endif
  349. let style = [s:settings.use_xhtml ? "" : '-->']
  350. let body_line = ''
  351. let html = []
  352. let s:html5 = 0
  353. if s:settings.use_xhtml
  354. call add(html, xml_line)
  355. endif
  356. if s:settings.use_xhtml
  357. call add(html, "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">")
  358. call add(html, '<html xmlns="http://www.w3.org/1999/xhtml">')
  359. elseif s:settings.use_css && !s:settings.no_pre
  360. call add(html, "<!DOCTYPE html>")
  361. call add(html, '<html>')
  362. let s:html5 = 1
  363. else
  364. call add(html, '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"')
  365. call add(html, ' "http://www.w3.org/TR/html4/loose.dtd">')
  366. call add(html, '<html>')
  367. endif
  368. call add(html, '<head>')
  369. " include encoding as close to the top as possible, but only if not already
  370. " contained in XML information
  371. if s:settings.encoding != "" && !s:settings.use_xhtml
  372. if s:html5
  373. call add(html, '<meta charset="' . s:settings.encoding . '"' . tag_close)
  374. else
  375. call add(html, "<meta http-equiv=\"content-type\" content=\"text/html; charset=" . s:settings.encoding . '"' . tag_close)
  376. endif
  377. endif
  378. call add(html, '<title>diff</title>')
  379. call add(html, '<meta name="Generator" content="Vim/'.v:version/100.'.'.v:version%100.'"'.tag_close)
  380. call add(html, '<meta name="plugin-version" content="'.g:loaded_2html_plugin.'"'.tag_close)
  381. call add(html, '<meta name="settings" content="'.
  382. \ join(filter(keys(s:settings),'s:settings[v:val]'),',').
  383. \ ',prevent_copy='.s:settings.prevent_copy.
  384. \ ',use_input_for_pc='.s:settings.use_input_for_pc.
  385. \ '"'.tag_close)
  386. call add(html, '<meta name="colorscheme" content="'.
  387. \ (exists('g:colors_name')
  388. \ ? g:colors_name
  389. \ : 'none'). '"'.tag_close)
  390. call add(html, '</head>')
  391. let body_line_num = len(html)
  392. call add(html, '<body'.(s:settings.line_ids ? ' onload="JumpToLine();"' : '').'>')
  393. call add(html, "<table ".(s:settings.use_css? "" : "border='1' width='100%' ")."id='vimCodeElement".s:settings.id_suffix."'>")
  394. call add(html, '<tr>')
  395. for buf in a:win_list
  396. call add(html, '<th>'.bufname(buf).'</th>')
  397. endfor
  398. call add(html, '</tr><tr>')
  399. let diff_style_start = 0
  400. let insert_index = 0
  401. for buf in a:buf_list
  402. let temp = []
  403. exe bufwinnr(buf) . 'wincmd w'
  404. " If text is folded because of user foldmethod settings, etc. we don't want
  405. " to act on everything in a fold by mistake.
  406. setlocal nofoldenable
  407. " When not using CSS or when using xhtml, the <body> line can be important.
  408. " Assume it will be the same for all buffers and grab it from the first
  409. " buffer. Similarly, need to grab the body end line as well.
  410. if body_line == ''
  411. 1
  412. call search('<body')
  413. let body_line = getline('.')
  414. $
  415. call search('</body>', 'b')
  416. let s:body_end_line = getline('.')
  417. endif
  418. " Grab the style information. Some of this will be duplicated so only insert
  419. " it if it's not already there. {{{
  420. 1
  421. let style_start = search('^<style\( type="text/css"\)\?>')
  422. 1
  423. let style_end = search('^</style>')
  424. if style_start > 0 && style_end > 0
  425. let buf_styles = getline(style_start + 1, style_end - 1)
  426. for a_style in buf_styles
  427. if index(style, a_style) == -1
  428. if diff_style_start == 0
  429. if a_style =~ '\<Diff\(Change\|Text\|Add\|Delete\)'
  430. let diff_style_start = len(style)-1
  431. endif
  432. endif
  433. call insert(style, a_style, insert_index)
  434. let insert_index += 1
  435. endif
  436. endfor
  437. endif " }}}
  438. " everything new will get added before the diff styles so diff highlight
  439. " properly overrides normal highlight
  440. if diff_style_start != 0
  441. let insert_index = diff_style_start
  442. endif
  443. " Delete those parts that are not needed so we can include the rest into the
  444. " resulting table.
  445. 1,/^<body.*\%(\n<!--.*-->\_s\+.*id='oneCharWidth'.*\_s\+.*id='oneInputWidth'.*\_s\+.*id='oneEmWidth'\)\?\zs/d_
  446. $
  447. ?</body>?,$d_
  448. let temp = getline(1,'$')
  449. " clean out id on the main content container because we already set it on
  450. " the table
  451. let temp[0] = substitute(temp[0], " id='vimCodeElement[^']*'", "", "")
  452. " undo deletion of start and end part
  453. " so we can later save the file as valid html
  454. " TODO: restore using grabbed lines if undolevel is 1?
  455. normal! 2u
  456. if s:settings.use_css
  457. call add(html, '<td><div>')
  458. elseif s:settings.use_xhtml
  459. call add(html, '<td nowrap="nowrap" valign="top"><div>')
  460. else
  461. call add(html, '<td nowrap valign="top"><div>')
  462. endif
  463. let html += temp
  464. call add(html, '</div></td>')
  465. " Close this buffer
  466. " TODO: the comment above says we're going to allow saving the file
  467. " later...but here we discard it?
  468. quit!
  469. endfor
  470. let html[body_line_num] = body_line
  471. call add(html, '</tr>')
  472. call add(html, '</table>')
  473. call add(html, s:body_end_line)
  474. call add(html, '</html>')
  475. " The generated HTML is admittedly ugly and takes a LONG time to fold.
  476. " Make sure the user doesn't do syntax folding when loading a generated file,
  477. " using a modeline.
  478. call add(html, '<!-- vim: set foldmethod=manual : -->')
  479. let i = 1
  480. let name = "Diff" . (s:settings.use_xhtml ? ".xhtml" : ".html")
  481. " Find an unused file name if current file name is already in use
  482. while filereadable(name)
  483. let name = substitute(name, '\d*\.x\?html$', '', '') . i . '.' . fnamemodify(copy(name), ":t:e")
  484. let i += 1
  485. endwhile
  486. let s:ei_sav = &eventignore
  487. set eventignore+=FileType
  488. exe "topleft new " . name
  489. let &eventignore=s:ei_sav
  490. unlet s:ei_sav
  491. setlocal modifiable
  492. " just in case some user autocmd creates content in the new buffer, make sure
  493. " it is empty before proceeding
  494. %d
  495. " set the fileencoding to match the charset we'll be using
  496. let &l:fileencoding=s:settings.vim_encoding
  497. " According to http://www.w3.org/TR/html4/charset.html#doc-char-set, the byte
  498. " order mark is highly recommend on the web when using multibyte encodings. But,
  499. " it is not a good idea to include it on UTF-8 files. Otherwise, let Vim
  500. " determine when it is actually inserted.
  501. if s:settings.vim_encoding == 'utf-8'
  502. setlocal nobomb
  503. else
  504. setlocal bomb
  505. endif
  506. call append(0, html)
  507. if len(style) > 0
  508. 1
  509. let style_start = search('^</head>')-1
  510. " add required javascript in reverse order so we can just call append again
  511. " and again without adjusting {{{
  512. let s:uses_script = s:settings.dynamic_folds || s:settings.line_ids
  513. " insert script closing tag if needed
  514. if s:uses_script
  515. call append(style_start, [
  516. \ '',
  517. \ s:settings.use_xhtml ? '//]]>' : '-->',
  518. \ "</script>"
  519. \ ])
  520. endif
  521. " insert javascript to get IDs from line numbers, and to open a fold before
  522. " jumping to any lines contained therein
  523. if s:settings.line_ids
  524. call append(style_start, [
  525. \ " /* Always jump to new location even if the line was hidden inside a fold, or",
  526. \ " * we corrected the raw number to a line ID.",
  527. \ " */",
  528. \ " if (lineElem) {",
  529. \ " lineElem.scrollIntoView(true);",
  530. \ " }",
  531. \ " return true;",
  532. \ "}",
  533. \ "if ('onhashchange' in window) {",
  534. \ " window.onhashchange = JumpToLine;",
  535. \ "}"
  536. \ ])
  537. if s:settings.dynamic_folds
  538. call append(style_start, [
  539. \ "",
  540. \ " /* navigate upwards in the DOM tree to open all folds containing the line */",
  541. \ " var node = lineElem;",
  542. \ " while (node && node.id != 'vimCodeElement".s:settings.id_suffix."')",
  543. \ " {",
  544. \ " if (node.className == 'closed-fold')",
  545. \ " {",
  546. \ " /* toggle open the fold ID (remove window ID) */",
  547. \ " toggleFold(node.id.substr(4));",
  548. \ " }",
  549. \ " node = node.parentNode;",
  550. \ " }",
  551. \ ])
  552. endif
  553. endif
  554. if s:settings.line_ids
  555. call append(style_start, [
  556. \ "",
  557. \ "/* function to open any folds containing a jumped-to line before jumping to it */",
  558. \ "function JumpToLine()",
  559. \ "{",
  560. \ " var lineNum;",
  561. \ " lineNum = window.location.hash;",
  562. \ " lineNum = lineNum.substr(1); /* strip off '#' */",
  563. \ "",
  564. \ " if (lineNum.indexOf('L') == -1) {",
  565. \ " lineNum = 'L'+lineNum;",
  566. \ " }",
  567. \ " if (lineNum.indexOf('W') == -1) {",
  568. \ " lineNum = 'W1'+lineNum;",
  569. \ " }",
  570. \ " var lineElem = document.getElementById(lineNum);"
  571. \ ])
  572. endif
  573. " Insert javascript to toggle matching folds open and closed in all windows,
  574. " if dynamic folding is active.
  575. if s:settings.dynamic_folds
  576. call append(style_start, [
  577. \ " function toggleFold(objID)",
  578. \ " {",
  579. \ " for (win_num = 1; win_num <= ".len(a:buf_list)."; win_num++)",
  580. \ " {",
  581. \ " var fold;",
  582. \ ' fold = document.getElementById("win"+win_num+objID);',
  583. \ " if(fold.className == 'closed-fold')",
  584. \ " {",
  585. \ " fold.className = 'open-fold';",
  586. \ " }",
  587. \ " else if (fold.className == 'open-fold')",
  588. \ " {",
  589. \ " fold.className = 'closed-fold';",
  590. \ " }",
  591. \ " }",
  592. \ " }",
  593. \ ])
  594. endif
  595. if s:uses_script
  596. " insert script tag if needed
  597. call append(style_start, [
  598. \ "<script" . (s:html5 ? "" : " type='text/javascript'") . ">",
  599. \ s:settings.use_xhtml ? '//<![CDATA[' : "<!--"])
  600. endif
  601. " Insert styles from all the generated html documents and additional styles
  602. " for the table-based layout of the side-by-side diff. The diff should take
  603. " up the full browser window (but not more), and be static in size,
  604. " horizontally scrollable when the lines are too long. Otherwise, the diff
  605. " is pretty useless for really long lines. {{{
  606. if s:settings.use_css
  607. call append(style_start,
  608. \ ['<style' . (s:html5 ? '' : 'type="text/css"') . '>']+
  609. \ style+
  610. \ [ s:settings.use_xhtml ? '' : '<!--',
  611. \ 'table { table-layout: fixed; }',
  612. \ 'html, body, table, tbody { width: 100%; margin: 0; padding: 0; }',
  613. \ 'table, td, th { border: 1px solid; }',
  614. \ 'td { vertical-align: top; }',
  615. \ 'th, td { width: '.printf("%.1f",100.0/len(a:win_list)).'%; }',
  616. \ 'td div { overflow: auto; }',
  617. \ s:settings.use_xhtml ? '' : '-->',
  618. \ '</style>'
  619. \])
  620. endif "}}}
  621. endif
  622. let &paste = s:old_paste
  623. let &magic = s:old_magic
  624. endfunc "}}}
  625. " Gets a single user option and sets it in the passed-in Dict, or gives it the
  626. " default value if the option doesn't actually exist.
  627. func! tohtml#GetOption(settings, option, default) "{{{
  628. if exists('g:html_'.a:option)
  629. let a:settings[a:option] = g:html_{a:option}
  630. else
  631. let a:settings[a:option] = a:default
  632. endif
  633. endfunc "}}}
  634. " returns a Dict containing the values of all user options for 2html, including
  635. " default values for those not given an explicit value by the user. Discards the
  636. " html_ prefix of the option for nicer looking code.
  637. func! tohtml#GetUserSettings() "{{{
  638. if exists('s:settings')
  639. " just restore the known options if we've already retrieved them
  640. return s:settings
  641. else
  642. " otherwise figure out which options are set
  643. let user_settings = {}
  644. " Define the correct option if the old option name exists and we haven't
  645. " already defined the correct one. Maybe I'll put out a warning message about
  646. " this sometime and remove the old option entirely at some even later time,
  647. " but for now just silently accept the old option.
  648. if exists('g:use_xhtml') && !exists("g:html_use_xhtml")
  649. let g:html_use_xhtml = g:use_xhtml
  650. endif
  651. " get current option settings with appropriate defaults {{{
  652. call tohtml#GetOption(user_settings, 'no_progress', !has("statusline") )
  653. call tohtml#GetOption(user_settings, 'diff_one_file', 0 )
  654. call tohtml#GetOption(user_settings, 'number_lines', &number )
  655. call tohtml#GetOption(user_settings, 'pre_wrap', &wrap )
  656. call tohtml#GetOption(user_settings, 'use_css', 1 )
  657. call tohtml#GetOption(user_settings, 'ignore_conceal', 0 )
  658. call tohtml#GetOption(user_settings, 'ignore_folding', 0 )
  659. call tohtml#GetOption(user_settings, 'dynamic_folds', 0 )
  660. call tohtml#GetOption(user_settings, 'no_foldcolumn', user_settings.ignore_folding)
  661. call tohtml#GetOption(user_settings, 'hover_unfold', 0 )
  662. call tohtml#GetOption(user_settings, 'no_pre', 0 )
  663. call tohtml#GetOption(user_settings, 'no_doc', 0 )
  664. call tohtml#GetOption(user_settings, 'no_links', 0 )
  665. call tohtml#GetOption(user_settings, 'no_modeline', 0 )
  666. call tohtml#GetOption(user_settings, 'no_invalid', 0 )
  667. call tohtml#GetOption(user_settings, 'whole_filler', 0 )
  668. call tohtml#GetOption(user_settings, 'use_xhtml', 0 )
  669. call tohtml#GetOption(user_settings, 'line_ids', user_settings.number_lines )
  670. call tohtml#GetOption(user_settings, 'use_input_for_pc', 'fallback')
  671. " }}}
  672. " override those settings that need it {{{
  673. " hover opening implies dynamic folding
  674. if user_settings.hover_unfold
  675. let user_settings.dynamic_folds = 1
  676. endif
  677. " ignore folding overrides dynamic folding
  678. if user_settings.ignore_folding && user_settings.dynamic_folds
  679. let user_settings.dynamic_folds = 0
  680. let user_settings.hover_unfold = 0
  681. endif
  682. " dynamic folding with no foldcolumn implies hover opens
  683. if user_settings.dynamic_folds && user_settings.no_foldcolumn
  684. let user_settings.hover_unfold = 1
  685. endif
  686. " dynamic folding implies css
  687. if user_settings.dynamic_folds
  688. let user_settings.use_css = 1
  689. else
  690. let user_settings.no_foldcolumn = 1 " won't do anything but for consistency and for the test suite
  691. endif
  692. " if we're not using CSS we cannot use a pre section because <font> tags
  693. " aren't allowed inside a <pre> block
  694. if !user_settings.use_css
  695. let user_settings.no_pre = 1
  696. endif
  697. " pre_wrap doesn't do anything if not using pre or not using CSS
  698. if user_settings.no_pre || !user_settings.use_css
  699. let user_settings.pre_wrap = 0
  700. endif
  701. "}}}
  702. " set up expand_tabs option after all the overrides so we know the
  703. " appropriate defaults {{{
  704. if user_settings.no_pre == 0
  705. call tohtml#GetOption(user_settings,
  706. \ 'expand_tabs',
  707. \ &expandtab || &ts != 8 || &vts != '' || user_settings.number_lines ||
  708. \ (user_settings.dynamic_folds && !user_settings.no_foldcolumn))
  709. else
  710. let user_settings.expand_tabs = 1
  711. endif
  712. " }}}
  713. " textual options
  714. if exists("g:html_use_encoding") "{{{
  715. " user specified the desired MIME charset, figure out proper
  716. " 'fileencoding' from it or warn the user if we cannot
  717. let user_settings.encoding = g:html_use_encoding
  718. let user_settings.vim_encoding = tohtml#EncodingFromCharset(g:html_use_encoding)
  719. if user_settings.vim_encoding == ''
  720. echohl WarningMsg
  721. echomsg "TOhtml: file encoding for"
  722. \ g:html_use_encoding
  723. \ "unknown, please set 'fileencoding'"
  724. echohl None
  725. endif
  726. else
  727. " Figure out proper MIME charset from 'fileencoding' if possible
  728. if &l:fileencoding != ''
  729. " If the buffer is not a "normal" type, the 'fileencoding' value may not
  730. " be trusted; since the buffer should not be written the fileencoding is
  731. " not intended to be used.
  732. if &l:buftype=='' || &l:buftype==?'help'
  733. let user_settings.vim_encoding = &l:fileencoding
  734. call tohtml#CharsetFromEncoding(user_settings)
  735. else
  736. let user_settings.encoding = '' " trigger detection using &encoding
  737. endif
  738. endif
  739. " else from 'encoding' if possible
  740. if &l:fileencoding == '' || user_settings.encoding == ''
  741. let user_settings.vim_encoding = &encoding
  742. call tohtml#CharsetFromEncoding(user_settings)
  743. endif
  744. " else default to UTF-8 and warn user
  745. if user_settings.encoding == ''
  746. let user_settings.vim_encoding = 'utf-8'
  747. let user_settings.encoding = 'UTF-8'
  748. echohl WarningMsg
  749. echomsg "TOhtml: couldn't determine MIME charset, using UTF-8"
  750. echohl None
  751. endif
  752. endif "}}}
  753. " Default to making nothing uncopyable, because we default to
  754. " not-standards way of doing things, and also because Microsoft Word and
  755. " others paste the <input> elements anyway.
  756. "
  757. " html_prevent_copy only has an effect when using CSS.
  758. "
  759. " All options:
  760. " f - fold column
  761. " n - line numbers (also within fold text)
  762. " t - fold text
  763. " d - diff filler
  764. " c - concealed text (reserved future)
  765. " l - listchars (reserved possible future)
  766. " s - signs (reserved possible future)
  767. "
  768. " Normal text is always selectable.
  769. let user_settings.prevent_copy = ""
  770. if user_settings.use_css
  771. if exists("g:html_prevent_copy")
  772. if user_settings.dynamic_folds && !user_settings.no_foldcolumn && g:html_prevent_copy =~# 'f'
  773. let user_settings.prevent_copy .= 'f'
  774. endif
  775. if user_settings.number_lines && g:html_prevent_copy =~# 'n'
  776. let user_settings.prevent_copy .= 'n'
  777. endif
  778. if &diff && g:html_prevent_copy =~# 'd'
  779. let user_settings.prevent_copy .= 'd'
  780. endif
  781. if !user_settings.ignore_folding && g:html_prevent_copy =~# 't'
  782. let user_settings.prevent_copy .= 't'
  783. endif
  784. else
  785. let user_settings.prevent_copy = ""
  786. endif
  787. endif
  788. if empty(user_settings.prevent_copy)
  789. let user_settings.no_invalid = 0
  790. endif
  791. " enforce valid values for use_input_for_pc
  792. if user_settings.use_input_for_pc !~# 'fallback\|none\|all'
  793. let user_settings.use_input_for_pc = 'fallback'
  794. echohl WarningMsg
  795. echomsg '2html: "' . g:html_use_input_for_pc . '" is not valid for g:html_use_input_for_pc'
  796. echomsg '2html: defaulting to "' . user_settings.use_input_for_pc . '"'
  797. echohl None
  798. sleep 3
  799. endif
  800. if exists('g:html_id_expr')
  801. let user_settings.id_suffix = eval(g:html_id_expr)
  802. if user_settings.id_suffix !~ '^[-_:.A-Za-z0-9]*$'
  803. echohl WarningMsg
  804. echomsg '2html: g:html_id_expr evaluated to invalid string for HTML id attributes'
  805. echomsg '2html: Omitting user-specified suffix'
  806. echohl None
  807. sleep 3
  808. let user_settings.id_suffix=""
  809. endif
  810. else
  811. let user_settings.id_suffix=""
  812. endif
  813. " TODO: font
  814. return user_settings
  815. endif
  816. endfunc "}}}
  817. " get the proper HTML charset name from a Vim encoding option.
  818. function! tohtml#CharsetFromEncoding(settings) "{{{
  819. let l:vim_encoding = a:settings.vim_encoding
  820. if exists('g:html_charset_override') && has_key(g:html_charset_override, l:vim_encoding)
  821. let a:settings.encoding = g:html_charset_override[l:vim_encoding]
  822. else
  823. if l:vim_encoding =~ '^8bit\|^2byte'
  824. " 8bit- and 2byte- prefixes are to indicate encodings available on the
  825. " system that Vim will convert with iconv(), look up just the encoding name,
  826. " not Vim's prefix.
  827. let l:vim_encoding = substitute(l:vim_encoding, '^8bit-\|^2byte-', '', '')
  828. endif
  829. if has_key(g:tohtml#encoding_to_charset, l:vim_encoding)
  830. let a:settings.encoding = g:tohtml#encoding_to_charset[l:vim_encoding]
  831. else
  832. let a:settings.encoding = ""
  833. endif
  834. endif
  835. if a:settings.encoding != ""
  836. let l:vim_encoding = tohtml#EncodingFromCharset(a:settings.encoding)
  837. if l:vim_encoding != ""
  838. " if the Vim encoding to HTML encoding conversion is set up (by default or
  839. " by the user) to convert to a different encoding, we need to also change
  840. " the Vim encoding of the new buffer
  841. let a:settings.vim_encoding = l:vim_encoding
  842. endif
  843. endif
  844. endfun "}}}
  845. " Get the proper Vim encoding option setting from an HTML charset name.
  846. function! tohtml#EncodingFromCharset(encoding) "{{{
  847. if exists('g:html_encoding_override') && has_key(g:html_encoding_override, a:encoding)
  848. return g:html_encoding_override[a:encoding]
  849. elseif has_key(g:tohtml#charset_to_encoding, tolower(a:encoding))
  850. return g:tohtml#charset_to_encoding[tolower(a:encoding)]
  851. else
  852. return ""
  853. endif
  854. endfun "}}}
  855. let &cpo = s:cpo_sav
  856. unlet s:cpo_sav
  857. " Make sure any patches will probably use consistent indent
  858. " vim: ts=8 sw=2 sts=2 noet fdm=marker