ref: e59909df1a72aff2c7bd8766e2c6237c78d693a8
author: sirjofri <sirjofri@sirjofri.de>
date: Wed Sep 8 02:09:25 EDT 2021
adds txt2ebook (basic functionality), txt2ms (original version as reference, still needs adjustment), and sample test.txt I'll provide a proper sample in a separate directory later.
--- /dev/null
+++ b/README
@@ -1,0 +1,5 @@
+E-Publishing on Plan 9
+
+* generating epub files
+* generating troff files
+* same source
--- /dev/null
+++ b/test.txt
@@ -1,0 +1,23 @@
+Sample Title
+
+# Sample Chapter
+
+Hello World
+Welcome here.
+
+# second chapter
+
+Content of second
+chapter
+
+- itemA
+- itemB
+- itemC
+
+## subsection
+
+content of subsection
+
+# section three
+
+whatever
--- /dev/null
+++ b/txt2ebook
@@ -1,0 +1,105 @@
+#!/bin/awk -f
+
+# if someone wants to use that:
+# ‥text‥ to print italic
+# ‥‥text‥‥ to print bold
+function parsed(a){
+ if(dontparse) return a
+ gsub(/‥‥[^‥]+‥‥/, "<b>&</b>", a)
+ gsub(/‥‥/, "", a)
+ gsub(/‥[^‥]+‥/, "<i>&</i>", a)
+ gsub(/‥/, "", a)
+ return a
+}
+
+BEGIN {
+ print "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
+ print "<html xmlns=\"http://www.w3.org/1999/xhtml\""
+ print " xmlns:ops=\"http://www.idpf.org/2007/ops\""
+ print " xml:lang=\"en\">"
+ print "<!-- end defs -->"
+}
+
+firstheader && NR == 1 {
+ Title = parsed($0)
+ print "<!-- begin header -->"
+ print "<head>"
+ printf " <title>%s</title>\n", Title
+ print "</head>"
+ print "<!-- end header -->"
+ print "<!-- begin body -->"
+ print "<body>"
+ printf "<h1>%s</h1>\n", Title
+ next
+}
+
+$1 ~ /^#+$/ {
+ l = length($1)
+ $1 = ""
+ sub(/^[ \t]*/, "")
+
+ if (isparagraph)
+ print " </p>"
+ while (issection >= l){
+ printf " </section><!-- %d -->\n", issection
+ issection--
+ }
+
+ if (l == 1)
+ printf " <section epub:type=\"chapter\"><!-- %d -->\n", l
+ if (l >= 2)
+ printf " <section><!-- %d -->\n", l
+
+ printf " <h%d>%s</h%d>\n\n", l+1, parsed($0), l+1
+ istitle = 1
+ issection = l
+ next
+}
+
+$1 ~ /^-/ {
+ $1 = ""
+ sub(/^[ \t]*/, "")
+ if (!isitem)
+ print "<ul>"
+ printf "<li>%s</li>\n", parsed($0)
+ isitem = 1
+ next
+}
+
+/^$/ {
+ istitle = 0
+ previousempty = 1
+
+ if (isitem) {
+ print "</ul>"
+ isitem = 0
+ }
+ if (isparagraph) {
+ print " </p>"
+ isparagraph = 0
+ }
+ next
+}
+
+previousempty {
+ printf " <p>"
+ print parsed($0)
+ previousempty = 0
+ isparagraph = 1
+}
+
+!previousempty {
+ printf " %s\n", parsed($0)
+}
+
+END {
+ print "<!-- END everything -->"
+ if (isparagraph)
+ print " </p>"
+ while (issection > 0){
+ printf " </section><!-- %d -->\n", issection
+ issection--
+ }
+ print "</body>"
+ print "</html>"
+}
--- /dev/null
+++ b/txt2ms
@@ -1,0 +1,66 @@
+#!/bin/awk -f
+
+# if someone wants to use that:
+# ‥text‥ to print italic
+# ‥‥text‥‥ to print bold
+# it might be better to just use \fI and \fB directly
+function parsed(a){
+ if(dontparse) return a
+ gsub(/‥‥[^‥]+‥‥/, "\\fB&\\fR", a)
+ gsub(/‥‥/, "", a)
+ gsub(/‥[^‥]+‥/, "\\fI&\\fR", a)
+ gsub(/‥/, "", a)
+ return a
+}
+
+BEGIN {
+ print ".de XS\n.ds LQ \"\"\n.nr |i (\\\\$1i-1i)/3u\n.ie '\\\\$1'0' .tm TOC:.XL \\\\$1 \\\\*(LQ\\\\$2\\\\*(LQ \\\\n%\n.el .tm TOC:.XL \\\\$1 \\\\*(LQ\\h'\\\\n(|iu'\\\\*(SN\\0\\0\\\\$2\\\\*(LQ \\\\n%\n.."
+ print ".tm TOC:.de XL\n.tm TOC:.nr |i \\\\\\\\$1\n.tm TOC:.ta \\\\\\\\n(LLuR\n.tm TOC:.ie \\\\\\\\$1>1 \\\\\n.tm TOC:\\\\{\\\\\n.tm TOC:\\\\\\\\$2 \\\\a\\\\|\\\\|\\\\\\\\$3\n.tm TOC:.vs 15pt\n.tm TOC:.\\\\}\n.tm TOC:.el \\\\\n.tm TOC:\\\\{\\\\\n.tm TOC:\\\\fB\\\\\\\\$2 \\\\|\\\\|\\\\\\\\$3\\\\fR\n.tm TOC:.vs 25pt\n.tm TOC:.\\\\}\n.tm TOC:.."
+ print ".tm TOC:.LP"
+}
+
+firstheader && NR == 1 {
+ printf ".TL\n%s\n", parsed($0)
+ nextline = 1
+}
+
+$1 ~ /^#+$/ {
+ l = length($1)
+ $1 = ""
+ sub(/^[ \t]*/, "")
+ printf ".NH %d\n.LG\n%s\n.NL\n", l, parsed($0)
+ printf ".XS %d \"%s\"\n", l, parsed($0)
+ istitle = 1
+ nextline = 1
+}
+
+$1 ~ /^\$+$/ {
+ $1 = ""
+ sub(/^[ \t]*/, "")
+ printf ".SH\n.LG\n%s\n.NL\n", parsed($0)
+ printf ".XS 0 \"%s\"\n", parsed($0)
+ istitle = 1
+ nextline = 1
+}
+
+$1 ~ /^-/ {
+ $1 = ""
+ sub(/^[ \t]*/, "")
+ printf ".IP •\n%s\n", parsed($0)
+ nextline = 1
+}
+
+/^$/ {
+ if(istitle)
+ printf ".LP"
+ else
+ printf ".PP"
+ istitle = 0
+}
+
+{
+ if(nextline == 0)
+ print parsed($0)
+ else
+ nextline = 0
+}