Gtk4-tutorial/lib/lib_add_head_tail_html.rb
2022-04-16 15:46:29 +09:00

50 lines
1.9 KiB
Ruby

# lib_add_head_tail_html.rb
# add header and tail to body (html)
def add_head_tail_html html_file
sample_md = <<~'EOS'
---
title: 'Gtk4 tutorial for beginners'
---
# sample header
Main contents begin here.
~~~{.C .numberLines}
int main(int argc, char **argv) {
}
~~~
|English|Japanese|
|:-----:|:------:|
|potato|jagaimo|
|carrot|ninjin|
|onion|tamanegi|
EOS
File.write "sample.md", sample_md
stat = system("pandoc", "-s", "-o", "sample.html", "sample.md")
File.delete("sample.md")
raise ("add_head_tail_html: pandoc retuns error status #{$?}.\n") unless stat == true
sample_html = File.read("sample.html")
File.delete("sample.html")
sample_html.gsub!(/<html xmlns="http:\/\/www.w3.org\/1999\/xhtml" lang="" xml:lang="">/,'<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">')
head = sample_html.partition(/<\/head>/)[0]
raise "No <style> tag in sample.html which is generated by pandoc." unless head.match?(/<style>.*<\/style>/m)
h1, h2, h3 = head.partition(/^\s*?<\/style>\s*?$/)
head = h1 + <<-'EOS' + h2 + h3 + "</head>\n<body>\n"
body {width: 1080px; margin: 0 auto; font-size: large;}
h2 {padding: 10px; background-color: #d0f0d0; }
div.sourceCode { margin: 10px; padding: 16px 10px 8px 10px; border: 2px solid silver; background-color: ghostwhite; overflow-x:scroll}
pre:not(.sourceCode) { margin: 10px; padding: 16px 10px 8px 10px; border: 2px solid silver; background-color: ghostwhite; overflow-x:scroll}
table {margin-left: auto; margin-right: auto; border-collapse: collapse; border: 1px solid;}
th {padding: 2px 6px; border: 1px solid; background-color: ghostwhite;}
td {padding: 2px 6px; border: 1px solid;}
img {display: block; margin-left: auto; margin-right: auto;}
figcaption {text-align: center;}
EOS
tail = "</body>\n</html>\n"
body = File.read(html_file)
File.write(html_file, head+body+tail)
end