shithub: epublish

ref: b6e0c25f3e1cfc89399fcb1611e463de55d0fa96
dir: /txt2ebook/

View raw version
#!/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:epub=\"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 ~ /^\[\[\[ebook/ {
	ismsblock = 1
	next
}

$1 ~ /^\]\]\]ebook/ {
	ismsblock = 0
	next
}

$1 ~ /^\[\[\[/ {
	ignore = 1
	next
}

$1 ~ /^\]\]\]/ {
	ignore = 0
	next
}

ignore {
	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
	next
}

!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>"
}