From cac75edaa0f08c2766cc865c03e623590ca764e3 Mon Sep 17 00:00:00 2001 From: Nero <41307858+nero@users.noreply.github.com> Date: Sun, 28 Nov 2021 17:26:47 +0000 Subject: [PATCH] Import into git --- md2html | 162 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ md2tex | 143 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 305 insertions(+) create mode 100755 md2html create mode 100755 md2tex diff --git a/md2html b/md2html new file mode 100755 index 0000000..d12d8b9 --- /dev/null +++ b/md2html @@ -0,0 +1,162 @@ +#!/usr/bin/awk -f +BEGIN { + indent = 0 + prefix = "html/body/" + print("") + level("html/head") +} + +function headers() { + printf("%0" indent "s\n", "") + if (meta["title"]) printf("%0" indent "s%s\n", "", escape(meta["title"])) + if (meta["author"]) printf("%0" indent "s\n", "", escape(meta["author"])) + if (stylesheet) printf("%0" indent "s\n", "", stylesheet) + if (javascript) printf("%0" indent "s\n", "", escape(javascript)) +} + +function oneliner(tag, str, args) { + printf("%0" indent "s<%s>%s\n", "", tag args, inline(str), tag) +} + +function level(new) { + split(new, a, "/") + split(old, b, "/") + + for (common = 0; common < 10; common++) { + if (common >= length(a)) break + if (common >= length(b)) break + if (a[common] != b[common]) break + } + + for (i = length(b); i >= common; --i) { + if (b[i]) { + if (b[i] == "head") headers() + indent = indent - 2 + printf("%0" indent "s\n", "", b[i]) + } + } + + for (i = common; i < length(a); i++) { + if (a[i]) { + printf("%0" indent "s<%s>\n", "", a[i]) + indent = indent + 2 + } + } + + old = new +} + +function escape(text) { + gsub(/&/, "\\&", text) + gsub(//, "\\>", text) + gsub(/\"/, "\\"", text) + return text +} + +function inline(text) { + text = escape(text) + text = gensub(/\*\*([^\*]*)\*\*/, "\\1", "g", text) + text = gensub(/\*([^\*]*)\*/, "\\1", "g", text) + text = gensub(/`([^`]*)`/, "\\1", "g", text) + return text +} + +/^---$/ { + parse_meta = !parse_meta + next +} + +{ + if(parse_meta) { + split($0, r, ": ") + gsub(/(^"|"$)/, "", r[2]) + meta[r[1]]=r[2] + next + } +} + +/^```/ { + if (old == prefix "pre") { + level(prefix) + } else { + level(prefix "pre") + } + next +} + +{ + if (old == prefix "pre") { + printf("%s\n", escape($0)) + next + } +} + +/^- / { + level(prefix "ul") + level(prefix "ul/li") + gsub(/^- /, "", $0) + printf("%0" indent "s%s\n", "", inline($0)) + next +} + +/^\s*\* / { + level(prefix "ul") + level(prefix "ul/li") + gsub(/^\s*\* /, "", $0) + printf("%0" indent "s%s\n", "", inline($0)) + next +} + +/^> / { + level(prefix "quote") + gsub(/^> /, "", $0) + printf("%0" indent "s%s\n", "", inline($0)) +} + +/^ / { + level(prefix "blockquote") + printf("%0" indent "s%s\n", "", inline($0)) +} + +/^ / { + if (old == prefix) level(prefix "p") + printf("%0" indent "s%s\n", "", inline($0)) + next +} + +/^# / { + gsub(/^#* /, "", $0) + if (!meta["title"]) meta["title"] = $0 + level(prefix) + oneliner("h1", $0) + next +} + +/^## / { + gsub(/^#* /, "", $0) + level(prefix) + oneliner("h2", $0) + next +} + +/^### / { + gsub(/^#* /, "", $0) + level(prefix) + oneliner("h3", $0) + next +} + +/^$/ { + if (old != "html/head") level(prefix) + next +} + +{ + level(prefix "p") + printf("%0" indent "s%s\n", "", inline($0)) + next +} +END { + level("") +} diff --git a/md2tex b/md2tex new file mode 100755 index 0000000..b1a72c1 --- /dev/null +++ b/md2tex @@ -0,0 +1,143 @@ +#!/usr/bin/awk -f +BEGIN { + headed = 0 + stack_pos = 0 + tag("documentclass[a4paper]{article}") +# tag("special{papersize=210mm,297mm}") + tag("usepackage[margin=1in]{geometry}") + tag("usepackage{listings}") + tag("lstset{basicstyle=\\ttfamily\\footnotesize,breaklines=true}") + tag("usepackage{hyperref}") + tag("usepackage{graphicx}") + tag("sloppy") + tag("parindent0mm") +} + +function tag(val) { + printf("\\%s\n", val) +} + +function push(what, arg) { + stack[stack_pos++] = what; + tag("begin{" what "}" arg) +} + +function peek() { + return stack[stack_pos-1] +} + +function pop() { + tag("end{" stack[--stack_pos] "}") +} + +function inline(text) { + gsub(/\\/, "\\textbackslash ", text) + gsub(/\{/, "\\{", text) + gsub(/\}/, "\\}", text) + gsub(/~/, "\\textasciitilde ", text) + gsub(/\$/, "\\$", text) + gsub(/_/, "\\_", text) + gsub(/#/, "\\#", text) + gsub(/%/, "\\%", text) + gsub(/&/, "\\&", text) + gsub(/\^/, "\\textasciicircum ", text) + gsub(/\.{3,}/, "\\ldots", text) + text = gensub(/\*\*([^\*]*)\*\*/, "\\textbf{\\1}", "g", text) + text = gensub(/\*([^\*]*)\*/, "\\emph{\\1}", "g", text) + text = gensub(/`([^`]*)`/, "\\texttt{\\1}", "g", text) + # links + text = gensub(/\[(.*)\]\((.*)\)/, "\\href{\\2}{\\1}\\footnote{\\url{\\2}}", "g", text) + return text +} + +/^---$/ { + parse_meta = !parse_meta + next +} + +{ + if(parse_meta) { + split($0, r, ": ") + gsub(/(^"|"$)/, "", r[2]) + meta[r[1]]=r[2] + next + } +} + +/^# / { + gsub(/^#* /, "", $0) + if (!meta["title"]) meta["title"] = $0 + next +} + +{ + if (!headed) { + if (meta["title"]) tag("title{" meta["title"] "}") + if (meta["author"]) tag("author{" meta["author"] "}") + push("document") + tag("maketitle") + headed = 1 + } +} + +/^## / { + gsub(/^#* /, "", $0) + tag("section{" $0 "}") + next +} + +/^### / { + gsub(/^#* /, "", $0) + tag("subsection{" $0 "}") + next +} + +/^```$/ { + if (peek() == "lstlisting") { + pop() + } else { + push("lstlisting") + } + next +} + +{ + if (peek() == "lstlisting") { + print($0) + next + } +} + +/^$/ { + while(peek() && peek() != "document") pop() +} + +/^!\[.*\]\(.*\)$/ { + txt = $0 + filenam = $0 + gsub(/(^!\[|\].*$)/, "", txt) + gsub(/(^!.*\(|\)$)/, "", filenam) + gsub(/\.(jpg|png|gif|jpeg)$/, "", filenam) + push("figure") + tag("includegraphics[width=\\textwidth]{" filenam "}") + tag("caption{" inline(txt) "}") + next +} + +/^- / { + gsub(/^- /, "", $0) + if (peek() != "itemize") push("itemize") + tag("item") + print(inline($0)) + next +} + +{ + print(inline($0)) +} + +END { + while (peek()) { + pop() + } +}