-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmdsplit
240 lines (211 loc) · 6.09 KB
/
mdsplit
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#!/usr/bin/awk -f
# Splits a large Markdown file into topics
# and creates a bookmap for use with LwDITA.
# If the file starts with a YAML block, the
# block gets printed as a keys file.
#
# Assumes a POSIX-ish system with awk and test available
#
# Usage: awk -f [me] [-v var=value] bigfile.md
# var:
# outdir - output directory (./out)
# keyfilename - keys file (keys.xml)
# bookfilename - book file (book.ditamap)
# maptitle - title/booktitle ("User Guide")
# bookdtd - bookmap DTD (standard OASIS DTD)
# mapdtd - map DTD (standard OASIS DTD)
# usemap - build generic (chapter?) map instead of bookmap (0)
#
BEGIN {
if(!keyfilename) keyfilename = "keys.xml";
if(!bookfilename) bookfilename = "book.ditamap";
if(!outdir) outdir = "out";
if(!maptitle) maptitle = "User Guide";
if(!bookdtd) bookdtd = "<!DOCTYPE bookmap PUBLIC \"-//OASIS//DTD DITA BookMap//EN\" \"bookmap.dtd\" [ ]>";
if(!mapdtd) mapdtd = "<!DOCTYPE map PUBLIC \"-//OASIS//DTD DITA Map//EN\" \"map.dtd\" [ ]>";
if(!usemap) usemap = 0;
dirtest = "test -d " outdir;
mkdir = "mkdir " outdir;
system( dirtest "||" mkdir ); # create the output directory if needed
# tab-delimited entries to build bookmap (level [tab] filename)
entries[1] = ""; entry = 1;
metadata[0] = 0; # number of metadata entries
inmeta = 0;
toodeep = 0;
}
# keep metadata (print at end as a key file)
# * Metadata has the following format:
# *
# * key: value
# *
# * This is true for all known YAML usage (Jekyll, MultiMarkdown, LwDITA).
NR == 1 && /^---/ {
inmeta = 1;
next;
}
/^---/ {
inmeta = 0;
next;
}
inmeta {
split( $0, a, /[:blank:]*:[:blank:]*/ );
metadata[a[1]] = a[2];
++metadata[0]; # count of keys detected
next;
}
# split Markdown files at headings
# * Special handling for headings:
# * We recognize the Pandoc ID/class tag
# * (e.g. {.class #id}.
# * Two specific class tags, `.section` and `.example`,
# * require special handling for LwDITA; they denote
# * subsections of a topic and thus need to stay with
# * the current topic/outfile being processed.
/^#+ / && $0 !~ /\.section/ && $0 !~ /\.example/ {
if( outfile ) close( outfile );
# what level?
if( $0 ~ /^# / ) lvl = 1;
if( $0 ~ /^## / ) lvl = 2;
if( $0 ~ /^### / ) lvl = 3;
if( $0 ~ /^#### / ) lvl = 4;
if( $0 ~ /^##### / ) lvl = 5;
# seriously... if you have 6+ levels, you need a re-think.
if( $0 ~ /^######+ / ) {
lvl = 5;
if( !toodeep ) {
print "Level 6+ heading detected, flattening to level 5.";
print "Please reconsider your document's structure and re-run."
toodeep = 1;
}
}
sub( /#+ /, "" ); # we're only using one hash in each topic
title = $0;
sub( /[:blank:]+{.*$/, "" ); # save ID marker in title but ditch in $0 for slugify
outfileroot = uniq_file( $0 ) ".md";
outfile = outdir "/" outfileroot;
print "#", title > outfile;
entries[entry] = lvl " " outfileroot; ++entry;
next;
}
# sub-headings (sections or examples)
/^##+ / && ($0 ~ /\{.*\.section/ || $0 ~ /\{.*\.example/) {
sub( /^##+/, "##" );
print >> outfile;
next;
}
{
# throw away blank lines & anything else between metadata and first heading
if(entry > 1) print $0 >> outfile;
next;
}
END {
if( outfile ) close( outfile );
# print metadata
if( metadata[0] ) {
outfile = outdir "/" keyfilename;
print "# keys {.concept}\n" > outfile;
for( i in metadata ) {
print "<p><ph id=\"" i "\">" metadata[i] "</ph></p>" >> outfile;
}
close(outfile);
}
# print bookmap/map
if( entry > 1 ) {
outfile = outdir "/" bookfilename;
print "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" >outfile;
if( usemap ) {
print mapdtd >> outfile;
print "<map>" >>outfile;
print " <title>" maptitle "</title>" >>outfile;
} else {
print bookdtd >> outfile;
print "<bookmap>" >>outfile;
print " <booktitle>" >>outfile;
print " <mainbooktitle>" maptitle "</mainbooktitle>" >>outfile;
print " </booktitle>" >>outfile;
if( metadata[0] )
print "<keydef keys=\"product\" href=\"keys.xml\"/>" >>outfile;
print " <frontmatter>\n <booklists>\n <toc toc=\"no\"/>\n</booklists>\n</frontmatter>" >>outfile;
}
chaps = 0;
lvl = 0;
for( i=1; i < entry; ++i ) {
split( entries[i], a, / / ); # split on tab
if( a[1] == 1 ) {
for( j = 2; j <= lvl; ++j ) print "</topicref>" >>outfile;
if( chaps ) {
if( !usemap ) {
print "</chapter>" >>outfile;
} else {
print "</topicref>" >>outfile;
}
}
++chaps;
lvl = 1;
if( chaps && !usemap ) {
print "<chapter href=\"" a[2] "\" format=\"markdown\">" >>outfile;
} else {
print "<topicref href=\"" a[2] "\" format=\"markdown\">" >>outfile;
}
} else {
for( j = a[1]; j <= lvl; ++j ) print "</topicref>" >>outfile;
lvl = a[1];
print "<topicref href=\"" a[2] "\" format=\"markdown\">" >>outfile;
}
}
for( j = 2; j <= lvl; ++j ) print "</topicref>" >>outfile;
if( chaps ) {
if( !usemap ) {
print "</chapter>" >>outfile;
} else {
print "</topicref>" >>outfile;
}
}
if( usemap ) {
print "</map>" >>outfile;
} else {
print "</bookmap>" >>outfile;
}
close(outfile);
}
}
function slugify( s, s1 ) {
s1 = tolower(s);
sub( / {.*/, "", s1 ); # dump type marker
gsub( / /, "_", s1 );
gsub( /[\-\(\):;!\/&'?]/, "", s1 );
gsub( /\*/, "_", s1 );
gsub( /_the_/, "_", s1 );
gsub( /^the_/, "", s1 );
gsub( /_for_/, "_", s1 );
gsub( /_as_/, "_", s1 );
gsub( /^to_/, "", s1 );
gsub( /_to_/, "_", s1 );
gsub( /_by_/, "_", s1 );
gsub( /^a_/, "", s1 );
gsub( /_a_/, "_", s1 );
gsub( /_an_/, "_", s1 );
gsub( /_is_/, "_", s1 );
gsub( /__*/, "_", s1 ); # collapse multiple underscores
sub( /_*$/, "", s1 ); # remove trailing underscores
return s1;
}
# scans output directory for matching files
# returns unique file root for each topic
function uniq_file( name, f, sys, x ) {
f = slugify(name);
sys = "test -f " outdir "/" f ".md";
if( system(sys) ) {
# return val >0 means filename doesn't exist, so it's unique
return f;
} else {
x = 0;
while(1) {
++x;
sys = "test -f " outdir "/" f "_" x ".md";
if( system(sys) ) break;
}
return f "_" x;
}
}
# vim: ts=4 sw=4 :