1"
2" cream-text2html.vim -- converts text files to basic HTML
3"
4" Cream -- An easy-to-use configuration of the famous Vim text editor
5" [ http://cream.sourceforge.net ] Copyright (C) 2001-2011 Steve Hall
6"
7" License:
8" This program is free software; you can redistribute it and/or modify
9" it under the terms of the GNU General Public License as published by
10" the Free Software Foundation; either version 3 of the License, or
11" (at your option) any later version.
12" [ http://www.gnu.org/licenses/gpl.html ]
13"
14" This program is distributed in the hope that it will be useful, but
15" WITHOUT ANY WARRANTY; without even the implied warranty of
16" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17" General Public License for more details.
18"
19" You should have received a copy of the GNU General Public License
20" along with this program; if not, write to the Free Software
21" Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
22" 02111-1307, USA.
23"
24" Description:
25" * Still very basic.
26" * Escapes offending characters
27" * Adds headers and footers
28" * Eventually, we want to be able to colorize based on syntax
29
30" register as a Cream add-on
31if exists("$CREAM")
32	call Cream_addon_register(
33	\ 'Convert Text to HTML',
34	\ 'Convert a text file to crude HTML.',
35	\ 'Convert a text file into a crude HTML equivilent. Escapes offending characters, spaces whitespace at line beginnings and adds headers and footers.',
36	\ 'Convert.Text to HTML',
37	\ 'call Cream_text2html()',
38	\ '<Nil>'
39	\ )
40endif
41
42function! Cream_text2html()
43" * Ampersands in the pattern are accepted literally
44" * Ampersands in the string must be double escaped(!?)
45
46	" capture cmdheight
47	let cmdheight = &cmdheight
48	" setting to 10+ avoids 'Press Enter...'
49	if cmdheight < 10
50		let &cmdheight = 10
51	endif
52
53	" save as [filename].html
54	" prompt
55	let m = confirm(
56		\ "SaveAs current file with \".html\" extension first?\n" .
57		\ "  (" . fnamemodify(expand("%"), ":p:t") . ".html)".
58		\ "\n", "&Ok\n&No", 1, "Info")
59	if m == 1
60		let n = Cream_appendext(expand("%", ":p"), "html")
61		if n == -1
62			call confirm(
63				\ "Saving file with \".html\" extension failed. Quitting...\n" .
64				\ "\n", "&Ok", 1, "Info")
65			" restore cmdheight
66			let &cmdheight = cmdheight
67			" quit
68			return
69		endif
70	endif
71
72	" convert all "&" to "&amp;"
73	execute ":%s/&/\\&amp;/ge"
74	" convert all """ to "&quot;")
75	execute ":%s/\"/\\&quot;/ge"
76	" convert all "<" to "&lt;")
77	execute ":%s/</\\&lt;/ge"
78	" convert all ">" to "&gt;")
79	execute ":%s/>/\\&gt;/ge"
80
81	" convert all tabs to character equivalent spaces
82	" TODO: convert spaces based on tabstop setting
83	execute ":%s/\t/\\&nbsp;\\&nbsp;\\&nbsp;\\&nbsp;/ge"
84
85	" convert all linebreaks to linebreaks with <br> tag
86	execute ":%s/\\n/<br>\<CR>/ge"
87	execute ":%s/<br>\\n\\n/<br>\<CR><br>\<CR>/ge"
88
89	" convert line leading spaces to "&nbsp;"
90	execute ":%s/\\n\\ \\ \\ \\ /\\r\\&nbsp;\\&nbsp;\\&nbsp;\\&nbsp;/ge"
91	execute ":%s/\\n\\ \\ \\ /\\r\\&nbsp;\\&nbsp;\\&nbsp;/ge"
92	execute ":%s/\\n\\ \\ /\\r\\&nbsp;\\&nbsp;/ge"
93	execute ":%s/\\n\\ /\\r\\&nbsp;/ge"
94
95	" add header HTML tags (at top of doc)
96	normal gg
97	normal O<html>
98	normal o<head>
99	normal o	<title></title>
100	normal o
101	normal <<
102	normal i</head>
103	normal o<body>
104	normal o
105	normal o<tt>
106	normal o
107
108	" add footer HTML tags (at bottom of doc)
109	normal G
110	normal o
111	normal o</tt>
112	normal o
113	normal o</body>
114	normal o</html>
115	normal o
116
117	" restore cmdheight
118	let &cmdheight = cmdheight
119
120endfunction
121
122