This repository has been archived by the owner on Jan 3, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhighlight.vim
115 lines (94 loc) · 3.07 KB
/
highlight.vim
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
" Vim syntax support file
"
" Maintainer: William Entriken <github.com@phor.net>
" Last Change: 2014 Sep 1
" Forked from: 2html.vim / Bram Moolenaar / 2003 May 31
"
" Transforms a file into HTML, using the current syntax highlighting.
" Outputs semantic HTML, with style to be applied separately
"
" Pass FILE as environmental variable to set output file
"
redir > cache/vimoutput.txt
let term="gui"
syn on
" Set some options to make it work faster.
" Expand tabs in original buffer to get 'tabstop' correctly used.
" Don't report changes for :substitute, there will be many of them.
set notitle noicon
setlocal et
set report=1000000
" Split window to create a buffer with the HTML file.
new $FILE
%d
set paste
set magic
wincmd p
let s:expandedtab = ' '
while strlen(s:expandedtab) < &ts
let s:expandedtab = s:expandedtab . ' '
endwhile
" Loop over all lines in the original text.
" Use html_start_line and html_end_line if they are set.
if exists("html_start_line")
let s:lnum = html_start_line
if s:lnum < 1 || s:lnum > line("$")
let s:lnum = 1
endif
else
let s:lnum = 1
endif
if exists("html_end_line")
let s:end = html_end_line
if s:end < s:lnum || s:end > line("$")
let s:end = line("$")
endif
else
let s:end = line("$")
endif
while s:lnum <= s:end
" Get the current line
let s:line = getline(s:lnum)
let s:len = strlen(s:line)
let s:new = ""
let s:new = '<span class="lnr">' . strpart(' ', 0, strlen(line("$")) - strlen(s:lnum)) . s:lnum . '</span> '
" Loop over each character in the line
let s:col = 1
while s:col <= s:len
let s:startcol = s:col " The start column for processing text
let s:id = synID(s:lnum, s:col, 1)
let s:col = s:col + 1
" Speed loop (it's small - that's the trick)
" Go along till we find a change in synID
while s:col <= s:len && s:id == synID(s:lnum, s:col, 1) | let s:col = s:col + 1 | endwhile
" Output the text with the same synID, with class set to {s:id_name}
let s:id = synIDtrans(s:id)
let s:id_name = synIDattr(s:id, "name", "gui")
let s:new = s:new . '<span class="' . s:id_name . '">' . substitute(substitute(substitute(substitute(substitute(strpart(s:line, s:startcol - 1, s:col - s:startcol), '&', '\&', 'g'), '<', '\<', 'g'), '>', '\>', 'g'), '"', '\"', 'g'), "\x0c", '<hr class="PAGE-BREAK">', 'g') . '</span>'
if s:col > s:len
break
endif
endwhile
" Expand tabs
let s:pad=0
let s:start = 0
let s:idx = stridx(s:line, "\t")
while s:idx >= 0
let s:i = &ts - ((s:start + s:pad + s:idx) % &ts)
let s:new = substitute(s:new, '\t', strpart(s:expandedtab, 0, s:i), '')
let s:pad = s:pad + s:i - 1
let s:start = s:start + s:idx + 1
let s:idx = stridx(strpart(s:line, s:start), "\t")
endwhile
exe "normal \<C-W>pa" . strtrans(s:new) . "\n\e\<C-W>p"
let s:lnum = s:lnum + 1
endwhile
" Finish with the last line
exe "normal \<C-W>pa\e"
" Now, when we finally know which, we define the colors and styles
" Add hyperlinks
%s+\(http://\S\{-}\)\(\([.,;:]\=\(\s\|$\)\)\|[\\"'<>]\)+<A HREF="\1">\1</A>\2+ge
" Cleanup
%s:\s\+$::e
wq
q