-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebpage-create.vim
More file actions
145 lines (130 loc) · 6.58 KB
/
Copy pathwebpage-create.vim
File metadata and controls
145 lines (130 loc) · 6.58 KB
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
" Source this file to load a function that will convert webpage.tex to html
function! s:tohtml() abort " {{{
let l:htmlroot = expand("%:p:h") . '/'
execute "cd" l:htmlroot
let l:htmlfile = l:htmlroot . "docs/" . expand("%:t:r") . ".html"
let l:cssfile = l:htmlroot . "docs/" . expand("%:t:r") . ".css"
let l:robotsfile = l:htmlroot . "docs/robots.txt"
" Get updated `robots.txt` file
call system('curl "https://raw.githubusercontent.com/ai-robots-txt/ai.robots.txt/refs/heads/main/robots.txt" > ' . l:robotsfile)
" First rerun latex, ensuring to use pdflatex
execute '!latexmk -pdf "%"'
" execute '!make4ht --config' l:htmlroot . 'webpage-create.cfg --output-dir' l:htmlroot . 'docs --utf8 --format html5+common_domfilters+latexmk_build %'
execute '!make4ht --config' l:htmlroot . 'webpage-create.cfg --output-dir' l:htmlroot . 'docs --utf8 %'
execute '!rm -f' l:htmlroot . expand("%:t:r") . ".html"
execute '!rm -f' l:htmlroot . expand("%:t:r") . ".css"
" Make various fixes to resulting .html file.
execute "edit" l:htmlfile
silent %substitute/\S\zs\s\+/ /ge
" Fix <body> tag to run JavaScript function
call search("<body>", "")
substitute /<body/\r<body onload='openSection("1")'/e
" Give it a title
call search("<title><\/title>")
substitute /></>CV: Bennett Helm</e
" Fix TOC: eliminate extra hyperlinks
1
while search("id='QQ", "W")
substitute /<a href=[^>]*>\([^<]*\)<\/a>/\1/e
endwhile
" Put jpgs of books into webpage
1
let l:savereg = @a
call search('Books<\/p><\/h2>')
let l:bookend = search('<\/dl>', 'n') " Find end of book section
while search('<dt class=[''"]thebibliography[''"] id=[''"]X\d\+-[^''"]*[''"]', 'We') && line('.') < l:bookend " Search for <dt> tag before end
" copy the book's bibtex id, whcih is used for the .jpg filename
let l:line = getline('.')
let l:filename = matchstr(l:line, 'id=[''"]X\d\+-\zs[^''"]*') . '.jpg'
call search('<dd\_.\{-}>', 'e') " Find after the <dd> tag
" Append the image in a div if the image exists
if findfile(l:filename, './docs') != ""
call append(line('.'), '<div class=''bookcontainer''><div class=''imgfloat''><div class=''imgborder''><img src=''' . l:filename . ''' style=''width:100%'' alt=''Book cover''></div></div>')
call search('<details') " Put end of div before abstract
normal! i</div>
endif
call search('<\/details>', 'e') " Add horizontal rule after abstract
normal! a<hr style='clear: both;'>
endwhile
let @a = l:savereg
" Add category buttons to article section
" Get complete list of categories and put <div> around list items
let l:categoryList = []
1
call search('Articles<\/h2>')
while(search('<span class=[''"]categories[''"]>', 'W'))
let [l:startLine, l:startPos] = searchpos('<span class=[''"]categories[''"]>', 'e')
let [l:endLine, l:endPos] = searchpos('<\/span>')
" Categories can span multiple lines. Assume at most l:endLine = l:startLine + 1
if l:startLine == l:endLine
let l:keywords = getline(l:startLine)[l:startPos:l:endPos - 2]
else
let l:keywords = getline(l:startLine)[l:startPos:]
let l:keywords .= getline(l:endLine)[:l:endPos - 2]
endif
" let l:keywords = search('<span class=[''"]categories[''"]>\zs\_.\{-}\ze<\/span>')
let l:keywordsList = split(l:keywords, ',')
call map(l:keywordsList, 'trim(v:val)') " Remove spaces around items
call map(l:keywordsList, 'substitute(v:val, "\\s\\+", " ", "g")') " Remove spaces within items
call extend(l:categoryList, l:keywordsList)
call map(l:keywordsList, 'substitute(v:val, "[^A-z]", "", "g")')
" Put <span> around the list item -- above `<dt...>` and below `</dd>`
" for filtering bib items by keyword
call search('<dt', 'b')
normal! i
execute '.,+substitute/thebibliography/thebibliography show filterBib ' . join(l:keywordsList)
call search('<\/dd>', 'e')
endwhile
"Clean up categoryList
call map(l:categoryList, 'trim(v:val)') " Remove spaces from all items
call sort(l:categoryList)
call uniq(l:categoryList)
" Create navigation/filter bar
let l:buttonClasses = "bibNav w3-button w3-round-large w3-border w3-border-teal w3-padding-small w3-hover-teal w3-small"
" l:buttonSpacer is needed to provide vertical spacing between lines of buttons
let l:buttonSpacer = ' <span style=''font-size:1.5em;''> </span>'
let l:bibNavBarList = ['<span id=''bibFilterContainer''>', ' <button class=''w3-teal showall ' . l:buttonClasses . ''' onclick=''filterBibliography("showall")''>Show all</button>', l:buttonSpacer]
for l:category in l:categoryList
let l:shortCategory = substitute(l:category, '[^A-z]', '', 'g')
call add(l:bibNavBarList, ' <button class=''' . l:shortCategory . ' ' . l:buttonClasses . ''' onclick=''filterBibliography("' . l:shortCategory . '")''> ' . l:category . '</button>')
call add(l:bibNavBarList, l:buttonSpacer)
endfor
call add(l:bibNavBarList, '</span>')
" Now add categories to appropriate location
call search('Articles<\/h2>', 'w')
call search('<dl class=[''"]thebibliography[''"]>')
call append(line('.') - 1, l:bibNavBarList)
" Remove space after opening quote (which happens for articles that have
" linked titles)
%substitute/“ /“/ge
" Convert HTML-coded double quotes
%substitute/'/"/ge
" " If using `marginyear=true`, put year into description field
" 1
" while(search('reversemarginpar', 'W')) " This finds marginyears
" normal dat " Delete around than <span>
" call search('.<\/dt>', 'bW') " Search for previous description tag
" normal P
" +
" endwhile
" OLD STUFF
" Put <h1>, <h2>, and <h3> tags at beginning of line
silent %substitute/<h[123]/\r&/ge
" Remove end-of-line spaces
silent %substitute/\s\+$//e
" Delete unneeded tags
silent %substitute/<a id=\_s*[^>]*><\/a>//ge
silent %substitute/<!-- l. \d\+ -->//ge
" Delete unneeded blank lines
silent %substitute/\n\+/\r/
" Put blank line before <div class='sectionDiv'> tags
global /sectionDiv/-put_
" Delete any blank lines at end of document
global /<\/html>/+,$delete_
update
" Open .html file
execute "!open -g" fnameescape(expand("%:r")) . ".html"
" Return to LaTeX file
edit #
endfunction " }}}
command! ToHtml :call <SID>tohtml()