Gtk4-tutorial/lib/lib_add_head_tail_html.rb

51 lines
1.9 KiB
Ruby
Raw Normal View History

# lib_add_head_tail_html.rb
# add header and tail to body (html)
def add_head_tail_html html_file
2022-04-16 08:46:29 +02:00
sample_md = <<~'EOS'
---
title: 'Gtk4 tutorial for beginners'
---
2022-04-16 08:46:29 +02:00
# sample header
2022-04-16 08:46:29 +02:00
Main contents begin here.
2022-04-16 08:46:29 +02:00
~~~{.C .numberLines}
int main(int argc, char **argv) {
}
~~~
2022-04-16 08:46:29 +02:00
|English|Japanese|
|:-----:|:------:|
|potato|jagaimo|
|carrot|ninjin|
|onion|tamanegi|
EOS
File.write "sample.md", sample_md
2022-04-16 08:46:29 +02:00
stat = system("pandoc", "-s", "-o", "sample.html", "sample.md")
2021-03-29 15:46:15 +02:00
File.delete("sample.md")
2022-04-16 08:46:29 +02:00
raise ("add_head_tail_html: pandoc retuns error status #{$?}.\n") unless stat == true
sample_html = File.read("sample.html")
2021-03-29 15:46:15 +02:00
File.delete("sample.html")
2022-04-16 08:46:29 +02:00
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