Improve multi-language support

This commit is contained in:
Nero 2022-07-02 20:55:45 +00:00
parent 121ebca9ae
commit 6c831dc78f
3 changed files with 27 additions and 9 deletions

View File

@ -7,7 +7,6 @@ Many people don't like to read markdown.
## Usage ## Usage
These are AWK scripts.
The input markdown goes into stdin. The input markdown goes into stdin.
The output document comes out of stdout. The output document comes out of stdout.
@ -24,12 +23,12 @@ pdflatex README.tex
If the awk option `stylesheet` is given, the HTML document can made prettier with external CSS. If the awk option `stylesheet` is given, the HTML document can made prettier with external CSS.
``` ```
awk -v stylesheet='/style.css' -f md2html < README.md md2html -v stylesheet='/style.css' < README.md
``` ```
## Supported constructs ## Supported constructs
- Headlines up to level 3 - Headlines up to level 6
- Paragraphs - Paragraphs
- Bold, italic and inline monospace - Bold, italic and inline monospace
- Image figures if they are in the same folder - Image figures if they are in the same folder
@ -40,7 +39,6 @@ awk -v stylesheet='/style.css' -f md2html < README.md
## Not supported ## Not supported
- Tables - Tables
- More than 3 levels of nested headlines
- Images from an url - Images from an url
- Inline images - Images inline in text
- Unicode characters (im german, maybe i will add it for umlauts) - Unicode characters (im german, maybe i will add at least umlaut support)

18
md2html
View File

@ -7,6 +7,7 @@ BEGIN {
prefix = "html/body/" prefix = "html/body/"
print("<!DOCTYPE html>") print("<!DOCTYPE html>")
level("html/head") level("html/head")
meta["lang"]="en"
toc=0 toc=0
} }
@ -46,7 +47,11 @@ function level(new) {
for (i = common; i < length(a); i++) { for (i = common; i < length(a); i++) {
if (a[i]) { if (a[i]) {
printf("%0" indent "s<%s>\n", "", a[i]) if (a[i]=="body") {
printf("%0" indent "s<%s lang=%s>\n", "", a[i], meta["lang"])
} else {
printf("%0" indent "s<%s>\n", "", a[i])
}
indent = indent + 2 indent = indent + 2
} }
} }
@ -188,6 +193,9 @@ awk '
BEGIN { BEGIN {
indent = 4 indent = 4
level("summary") level("summary")
lang="en"
tocname["en"]="Table of contents"
tocname["de"]="Inhaltsverzeichnis"
} }
function oneliner(tag, str, args) { function oneliner(tag, str, args) {
@ -242,6 +250,14 @@ function inline(text) {
return text return text
} }
/<body lang=/ {
lang=$0
gsub(/^.*lang=\"*/,"",lang)
gsub(/[^a-z].*$/,"",lang)
level("summary")
oneliner("h2", tocname[lang])
}
/<h[2-6] / { /<h[2-6] / {
id=$0 id=$0
gsub(/^.*id=\"/,"",id) gsub(/^.*id=\"/,"",id)

8
md2tex
View File

@ -5,6 +5,7 @@ BEGIN {
tag("documentclass[a4paper]{article}") tag("documentclass[a4paper]{article}")
# tag("special{papersize=210mm,297mm}") # tag("special{papersize=210mm,297mm}")
tag("usepackage[margin=1in]{geometry}") tag("usepackage[margin=1in]{geometry}")
tag("usepackage[utf8]{inputenc}")
tag("setlength{\\parskip}{\\baselineskip}%") tag("setlength{\\parskip}{\\baselineskip}%")
tag("setlength{\\parindent}{0pt}%") tag("setlength{\\parindent}{0pt}%")
tag("usepackage{listings}") tag("usepackage{listings}")
@ -82,9 +83,12 @@ function inline(text) {
} }
} }
/^## / { /^##* / {
d=length($1)
t="section"
for (i=3; i<=d; i++) t="sub"t
gsub(/^#* /, "", $0) gsub(/^#* /, "", $0)
tag("section{" $0 "}") tag(t"{" $0 "}")
next next
} }