@@ -3,7 +3,8 @@ System.IO.Directory.SetCurrentDirectory __SOURCE_DIRECTORY__
3
3
// ==============================================
4
4
// Script file to fixup files for publishing to GitBook
5
5
//
6
- // == Inputs and outputs ==
6
+ // The idea is that I can copy the source .md files from the Jekyll-based static site
7
+ // into the subdirectories here and then fix them up with this script
7
8
//
8
9
// The tool will process all code under the parent directory (../) by default
9
10
@@ -18,6 +19,8 @@ module Helpers =
18
19
Regex.Replace( input, pattern, replacement)
19
20
20
21
(*
22
+ // test the regexes
23
+
21
24
replaceRegex @"(?m)^\{\%\s+highlight.*?$" "```" "{% highlight fsharp %}\r\nabc"
22
25
replaceRegex @"(?m)^\d\d\d\d-\d\d-\d\d-(.*?$)" "$1" "2012-09-01-myfile.md"
23
26
@@ -32,11 +35,18 @@ module Helpers =
32
35
33
36
let ifNone v opt = defaultArg opt v
34
37
38
+ // ==========================
39
+ // Rename and organize files
40
+ //
41
+ // This works only with newly copied files.
42
+ // Any existing files must be deleted first.
43
+ // ==========================
35
44
36
45
module FixupFiles =
37
46
38
47
let rec fixupFileNames ( d : DirectoryInfo ) =
39
48
49
+ // remove date "2014-08-12-XXX" prefix from files
40
50
let renameIfNeeded ( fi : FileInfo ) =
41
51
let oldName = fi.Name
42
52
let newName = replaceRegex " (?m)^\d\d\d\d -\d\d -\d\d -(.*?$)" " $1" oldName
@@ -54,11 +64,20 @@ module FixupFiles =
54
64
d.EnumerateDirectories()
55
65
|> Seq.iter fixupFileNames
56
66
67
+ // ==========================
68
+ // Fixup the text in the files
69
+ //
70
+ // * Replace Liquid code block markup {%highlight%} with GH markup (```)
71
+ // * Replace absolute paths with relative ones, e.g. for /posts/ and /series/
72
+ // * For video pages, replace relative ones with online absolute http links
73
+ // * Fixup the smart quotes into ASCII friendly ones.
74
+ // ==========================
75
+
57
76
module FixupText =
58
77
59
78
let replaceCodeBlockDelimiters text =
60
79
text
61
- |> replaceRegex " (?m)^\{\%\s +highlight.*? $" " ```"
80
+ |> replaceRegex " (?m)^\{\%\s +highlight\s *( \S *).* \%\} .* $" " ```$1 "
62
81
|> replaceRegex " (?m)^\{\%\s +endhighlight.*?$" " ```"
63
82
64
83
let fixupLinkPaths text =
@@ -110,6 +129,14 @@ module FixupText =
110
129
|> Seq.iter fixupDirectory
111
130
112
131
132
+ // ==========================
133
+ // Fixup the series pages
134
+ //
135
+ // In the Jekyll blog, the links for series pages are generated in the template
136
+ // Instead, generate them explicitly here and append to the series page
137
+ //
138
+ // Note this should only be run once, otherwise another TOC will be appended!
139
+ // ==========================
113
140
114
141
module Series =
115
142
@@ -135,6 +162,8 @@ module Series =
135
162
None
136
163
)
137
164
(*
165
+ // test the regexes
166
+
138
167
let pattern = """title:\s*"([^"]+)"\s*"""
139
168
let m = Regex.Match("""title:"abc" """,pattern) in m.Value
140
169
let m = Regex.Match("""title:"abc" """,pattern) in m.Groups.[1].Value
@@ -165,6 +194,8 @@ module Series =
165
194
}
166
195
167
196
(*
197
+ // test the code
198
+
168
199
let fi = FileInfo("..")
169
200
let lines =
170
201
[
@@ -198,6 +229,7 @@ module Series =
198
229
|> List.choose ( fun ( keyOpt , vals ) -> keyOpt |> Option.map ( fun key -> key, vals))
199
230
|> Map.ofList
200
231
232
+ // test
201
233
// Series.seriesIdToPostPages()
202
234
203
235
let seriesIdToSeriesIndexPages () =
@@ -208,6 +240,7 @@ module Series =
208
240
|> Seq.toList
209
241
|> List.choose ( fun page -> page.SeriesIndexId |> Option.map ( fun key -> key, page))
210
242
243
+ // test
211
244
// Series.seriesIdToSeriesIndexPages()
212
245
213
246
type SeriesInfo = {
@@ -236,6 +269,16 @@ module Series =
236
269
{ SeriesPage= seriesPage; PostPages= postPages}
237
270
)
238
271
|> List.iter updateSeriesPageContent
272
+
273
+ // ==========================
274
+ // Ensure all the images exist
275
+ //
276
+ // In the Jekyll blog, there are many image files. Only some are needed here.
277
+ // So collect the image links from the posts, and check if they exist locally,
278
+ // then emit a list of missing ones.
279
+ //
280
+ // Missing files are then copied over manually.
281
+ // ==========================
239
282
240
283
module Images =
241
284
@@ -258,15 +301,15 @@ module Images =
258
301
imagePath
259
302
|> File.Exists
260
303
261
- let rec findImages ( d : DirectoryInfo ) =
304
+ let rec collectMissingImages ( d : DirectoryInfo ) =
262
305
seq {
263
306
yield !
264
307
d.EnumerateFiles( " *.md" )
265
308
|> Seq.collect findImagesInFile
266
309
267
310
yield !
268
311
d.EnumerateDirectories()
269
- |> Seq.collect findImages
312
+ |> Seq.collect collectMissingImages
270
313
}
271
314
|> Seq.distinct
272
315
|> Seq.toList
@@ -282,4 +325,4 @@ FixupText.fixupDirectory d
282
325
283
326
// Series.updateSeriesInfoPages()
284
327
285
- Images.findImages d
328
+ Images.collectMissingImages d
0 commit comments