wm: infra

ref: a093dbb53911954c664b4ebfa0457f5f46011cda
dir: /doc/wiki2ms/

View raw version
#!/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
		if(~ $i .P2)
			mono=f
		if(~ $mono f)
			echo $i | sed 's/\[([^\n]+)\|([^\n]+)\]/\n\.LN \2\n\1\n\.LN\n/g'
	}	
}

fn indirectlink {
	ifs=$nl
	mono=f
	for(i in `{cat}){
		if(~ $i .P1)
			mono=t
		if(~ $i .P2)
			mono=f
		if(~ $mono f)
			echo $i | sed 's/\[([^\n]+)\]/\n\.LN \1\n\1\n\.LN\n/g'
	}
}

fn manpage {
	ifs=$nl
	mono=f
	for(i in `{cat}){
		if(~ $i .P1)
			mono=t
		if(~ $i .P2)
			mono=f
		if(~ $mono f)
			echo $i | sed 's/([^ ]+)\(([0-9])\)/\n\.LN '^$url^'\2\/\1\n\1\(\2\)\n\.LN\n/g'
	}
}

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
manpage