File: 2html.vim

package info (click to toggle)
vim-rt 5.3-12
  • links: PTS
  • area: main
  • in suites: slink
  • size: 3,172 kB
  • ctags: 815
  • sloc: makefile: 857; awk: 778; ansic: 379; perl: 192; sh: 167
file content (147 lines) | stat: -rw-r--r-- 3,557 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
" Vim syntax support file
" Maintainer:	Bram Moolenaar <Bram@vim.org>
" Last change:	1998 Jul 25

" Transform a file into HTML, using the current syntax highlighting.

" Split window to create a buffer with the HTML file.
if expand("%") == ""
  new Untitled.html
else
  new %.html
endif
1,$d
let old_title = &title
let old_icon = &icon
let old_paste = &paste
set notitle noicon paste noet

" Find out the background and foreground color.
let bg = synIDattr(highlightID("Normal"), "bg#", "gui")
let fg = synIDattr(highlightID("Normal"), "fg#", "gui")
if bg == ""
   if &background == "dark"
     let bg = "#000000"
     if fg == ""
       let fg = "#FFFFFF"
     endif
   else
     let bg = "#FFFFFF"
     if fg == ""
       let fg = "#000000"
     endif
   endif
endif

" Insert HTML header, with the background color.  Add the foreground color
" only when it is defined.
exec "normal a<HTML>\n<HEAD>\n<TITLE>".expand("%:t")."</TITLE>\n</HEAD>\n<BODY BGCOLOR=".bg."\e"
if fg != ""
  exec "normal a TEXT=".fg."\e"
endif
exec "normal a>\n<PRE>\n\e"

exec "normal \<C-W>p"

" Loop over all lines in the buffer
let lnum = 1
while lnum <= line("$")
  let col = 1
  let startcol = 1
  let id = 0
  let line = getline(lnum)
  let len = strlen(line)
  let new = ""
  let color = ""
  let bgcolor = ""
  let bold = ""
  let italic = ""
  let underline = ""

  " Loop over each character in the line, plus just after the last character
  while col <= len + 1

    " Get the syntax ID of the current character
    if col > len
      let new_id = 0
    else
      let new_id = synID(lnum, col, 1)
    endif

    " When finding a different syntax ID, generate the text up to here.
    if new_id != id || col > len
      while startcol < col
        let c = line[startcol - 1]
	" replace special HTML characters
	if c == "<"
	  let new = new . "&lt;"
	elseif c == ">"
	  let new = new . "&gt;"
	elseif c == "&"
	  let new = new . "&amp;"
	elseif c == '"'
	  let new = new . "&quot;"
	elseif c == "\x0C"
	  let new = new . "<HR class=PAGE-BREAK>"
	else
	  let new = new . c
	endif
	let startcol = startcol + 1
      endwhile

      " Stop any highlighting
      if color != ""
        let new = new . "</FONT>"
      endif
      if bgcolor != ""
        let new = new . "</SPAN>"
      endif
      if bold == "1"
        let new = new . "</B>"
      endif
      if italic == "1"
        let new = new . "</I>"
      endif
      if underline == "1"
        let new = new . "</U>"
      endif
    endif

    " If the following text has highlighing, start it
    if new_id != id && col <= len
      let id = new_id
      let underline = synIDattr(synIDtrans(id), "underline", "gui")
      if underline == "1"
        let new = new . "<U>"
      endif
      let italic = synIDattr(synIDtrans(id), "italic", "gui")
      if italic == "1"
        let new = new . "<I>"
      endif
      let bold = synIDattr(synIDtrans(id), "bold", "gui")
      if bold == "1"
        let new = new . "<B>"
      endif
      let bgcolor = synIDattr(synIDtrans(id), "bg#", "gui")
      if bgcolor != ""
        let new = new . '<SPAN style="background-color: ' . bgcolor . '">'
      endif
      let color = synIDattr(synIDtrans(id), "fg#", "gui")
      if color != ""
	let new = new . "<FONT COLOR=" . color . ">"
      endif
    endif
    let col = col + 1
  endwhile

  exec "normal \<C-W>pa" . new . "\n\e\<C-W>p"

  let lnum = lnum + 1
endwhile

" Finish with the last line
exec "normal \<C-W>pa</PRE>\n</BODY>\n</HTML>\e"

let &title = old_title
let &icon = old_icon
let &paste = old_paste