-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprettify.awk
executable file
·151 lines (139 loc) · 4.91 KB
/
prettify.awk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
function htmlsanitise(a){
result = gensub(/</, "\\<", "g", a)
result = gensub(/>/, "\\>", "g", result)
return result
}
# takes a spoken/printed line, returns formatted and ready text
function get_text(line){
split(line,linearr,"\"")
ln = linearr[2]
if (ln ~ /[<>]/){
ln = htmlsanitise(ln)
}
if (ln ~ /[()]/){
ln = gensub(/([一-龯]+)(([一-龯]+))/, "<ruby>\\1<rt>\\2</rt></ruby>", "g", ln)
}
return "<p>" ln "</p>"
}
# takes line depicting character name in modded higurashi scripts, returns formatted name text
function get_charname(line){
split(line, chararr, ">|<")
sub("=", ":", chararr[2])
return "<p style=\"font-weight: bold;\"> <span style=\"" chararr[2] "; \"> ♦ </span>" chararr[3] "</p>"
}
# takes a filename and subroutine name & writes out the content of that sub to the output file
function get_external_text(ext_filename, ext_subname){
# read until specified sub is encountered
do {
morelines = getline < ext_filename
if ($0 ~ ext_subname){break}
} while (morelines)
hangingOpenBrackets = 0
hangingOpenBrackets += gsub("{","") # just in case the first bracket is opened on the sub name line
hangingOpenBrackets -= gsub("}","")
# read relevant lines into output text file until end of sub or file is encountered
do {
morelines = getline < ext_filename
if ($0 ~ /ClearMessage/){
print "\n<p><br/></p>\n" > outputtxt
print " - break - "
}
if ($0 ~ /GADVMode.*OutputLine\>.+color=/){
print get_charname($0) > outputtxt
print get_charname($0)
}
if ($0 ~ /OutputLine\(NULL/){
print get_text($0) > outputtxt
print "line: " get_text($0)
}
if ($0 ~ /^void dialog/){break}
hangingOpenBrackets += gsub("{","")
hangingOpenBrackets -= gsub("}","")
} while (morelines && hangingOpenBrackets)
close(ext_filename) # ensure file is re-opened and read from the beginning next time
}
BEGIN {
FS = "\""
maintxtfile = "p-001.txt"
mainxhtmlfile = "p-001.xhtml"
maintxt = "book-template/item/xhtml/" maintxtfile
tipstxtfile = "tips.txt"
tipsxhtmlfile = "tips.xhtml"
tipstxt = "book-template/item/xhtml/" tipstxtfile
++tocid
tocs[tocid] = "イントロ"
print "<h2 class=\"oo-midashi\" id=\"toc-" tocid "\">" tocs[tocid] "</h2>\n<p><br/></p>" > maintxt
}
BEGINFILE {
if (tolower(FILENAME) ~ /^[^z].+tip.+txt$/){
# file is a tips file, link to tip footnote & set output variable to footnote file
outputtxt = tipstxt
print "\n<br/>\n<aside class=\"footnote\" epub:type=\"footnote\" id=\"tip" ++tipno "\">" > outputtxt
} else {
# file is normal story script, set output variable to main body text file
outputtxt = maintxt
print "\n<br/>\n" > outputtxt
}
found = 0
}
# skip files starting z, containing 'tip', and ending txt (original/censored text files from higurashi mod)
{if (tolower(FILENAME) ~ /^z.+tip.+txt$/) {nextfile}}
# get titles for table of contents (also format titles)
/■/{
if (! found) {
found = 1
titletext = gensub(/.*■(.+).$/, "\\1", 1)
if (tolower(FILENAME) ~ /tips/){
print "<h2 class=\"oo-midashi\">" titletext "</h2>\n<p><br/></p>" > outputtxt
++tipno_backref_id
} else {
print "<h2 class=\"oo-midashi\" id=\"toc-" ++tocid "\">" titletext "</h2>\n<p><br/></p>" > outputtxt
tocs[tocid] = titletext
}
}
}
/ClearMessage/{
print "\n<p><br/></p>\n" > outputtxt
}
# Include the name of the character who is speaking (when available in modded script)
/GADVMode.*OutputLine\>.+color=/{
print get_charname($0) > outputtxt
}
/OutputLine\(NULL/{
print get_text($0) > outputtxt
}
# retrieve content loaded from external scripts in 07th-mod modded version of higurashi
/ModCallScriptSection/ {
if($2 ~ /&/){next} # filter out filenames containing "&" character
gtet = strtonum(substr($1, match($1, "[0-9]"), 1))
if (censor_level >= gtet) {
ext_filename = $2 ".txt"
ext_subname = $4
}
getline
ltet = strtonum(substr($1, match($1, "[0-9]"), 1))
if (censor_level <= ltet) {
ext_filename = $2 ".txt"
ext_subname = $4
}
print "mod ext file and sub name: " ext_filename " - " ext_subname
get_external_text(ext_filename, ext_subname)
}
# place tip footnote link in main body text
/TIPS_NEW/{
print "<p>Tip <a class=\"noteref\" epub:type=\"noteref\" id=\"tip_body-" ++tipno_body "\" href=\"" tipsxhtmlfile "#tip" tipno_body "\">" tipno_body "</a></p>" > outputtxt
}
ENDFILE {
# assume tip files starting with "z" are in fact mod scripts
if (tolower(FILENAME) ~ /^[^z].+tip.+txt$/){
# place link back to main body text at end of tip footnote & close footnote aside tag
print "<a href=\"" mainxhtmlfile "#tip_body-" tipno_backref_id "\">戻る</a></aside>" > outputtxt
print "\n<br/>\n" > outputtxt
}
}
END{
for (i = 1; i <= tocid; ++i){
print "<li><a href=\"xhtml/p-001.xhtml#toc-" i "\">" tocs[i] "</a></li>" > "book-template/item/navigation-documents.txt"
print "<p><a href=\"p-001.xhtml#toc-" i "\">" tocs[i] "</a></p>" > "book-template/item/xhtml/p-toc.txt"
}
}