ref: e61f14729a8087126515a81c94a3d95f7aa79ac8
dir: /plan9/doc/wiki2ms/
#!/bin/rc
nl='
'
file=$1
url=$2
# wiki2ms
# convert from wikifs(6) format to ms format
# usage:
# wiki2ms document.wiki https://cloud9p.org/man # normal wiki text
# wiki2ms document.wms https://cloud9p.org/man # wiki with ms macros
# bugs:
# man page linking is better to be optional
# very wasteful
# ms output could've been better
# some of wikifs functionality is not yet implmented:
# proper parsing for links
fn quote {
echo $1 | sed 's/\//\\\//g' | sed 's/\./\\\./g'
}
fn comment {
ifs=$nl
for(i in `{cat}){
if(! ~ $i '#'*)
echo $i
}
}
fn removebang {
# wasteful, i know
echo $1 | sed 's/^(! |!)//g'
}
fn monospace {
ifs=$nl
mono=f
for(i in `{cat}){
if(~ $i !*){
if(~ $mono f){
mono=t
echo '.P1'
}
removebang $i
}
if not {
if(~ $mono t) {
mono=f
echo '.P2'
}
echo $i
}
}
# last line may be a ! line.
if(~ $mono t){
mono=f
echo '.P2'
}
}
fn directlink {
ifs=$nl
mono=f
for(i in `{cat}){
if(~ $i .P1){
mono=t
echo $i
}
if not if(~ $i .P2){
mono=f
echo $i
}
if not if(~ $mono f)
echo $i | sed 's/\[([^\n]+)\|([^\n]+)\]/\n\.LN \2\n\1\n\.LN\n/g'
if not
echo $i
}
}
fn indirectlink {
ifs=$nl
mono=f
for(i in `{cat}){
if(~ $i .P1){
mono=t
echo $i
}
if not if(~ $i .P2){
mono=f
echo $i
}
if not if(~ $mono f)
echo $i | sed 's/\[([^\n]+)\]/\n\.LN \1\n\1\n\.LN\n/g'
if not
echo $i
}
}
fn manpage {
ifs=$nl
mono=f
for(i in `{cat}){
if(~ $i .P1){
mono=t
echo $i
}
if not if(~ $i .P2){
mono=f
echo $i
}
if not if(~ $mono f)
echo $i | sed 's/([^ ]+)\(([0-9])\)/\n\.LN '^$url^'\2\/\1\n\1\(\2\)\n\.LN\n/g'
if not
echo $i
}
}
url=`{quote $url}
cat $1 |
# paragraphs
sed 's/^$/.LP/g' |
# comments
comment |
# titles
sed 's/^((([A-Z]| |-|_|\(|\)))+)$/\.SH\n\1\n\.LP/g' |
# bulleted lists
sed 's/(^\* )|(^\*)/\.IP \\(bu\n/g' |
# monospaced block
monospace |
# direct links ([x|y] form)
directlink |
# indirect links ([x] form)
indirectlink |
# man pages, TODO: enable it later
#manpage
cat