-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathpreprocess.ps1
More file actions
294 lines (242 loc) · 11.3 KB
/
preprocess.ps1
File metadata and controls
294 lines (242 loc) · 11.3 KB
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
param(
[string]$pageName,
[string]$pagePath,
[string]$templateName,
[string]$templatePath,
[string]$mediawikiDomain
)
$variablesPath = $PSScriptRoot + '\.\Variables.ps1'
. $variablesPath
$mailToOrg = '@microsoft.com'
$mediaWikiPageNameKeyword = "PAGENAME"
function preProcessPage($path, $pageName) {
$content = Get-Content $path -Raw
$content = preProcessPageContent -content $content -pageName $pageName
Set-Content -Path $path -Value $content
}
function preprocessTemplate($path, $pageName) {
$content = Get-Content $path -Raw
$content = preProcessTemplateContent -content $content
$content = preProcessPageContent -content $content -pageName $pageName
Set-Content -Path $path -Value $content
}
function preProcessPageContent($content, $pageName) {
## replace [[http...]] with [http...] for pandoc to migrate correclty
$content = $content -replace '(\[\[http)(((?!\]\]).)*)(\]\])','[http$2]'
## replace [[file:...]] with [[File:...]] for pandoc to migrate correclty
$content = $content -replace '(\[\[file:)(((?!\]\]).)*)(\]\])','[[File:$2]]'
# usually we dont show no-include content for templates but for content pages, show no include content as well
$content = $content -replace '<noinclude>((.|\n)*?)<\/noinclude>','$1'
#remove includeonly tag and preserve its content
$content = $content -replace '<includeonly>((.|\n)*?)<\/includeonly>','$1'
#handle some common mistakes that pandoc does not understand
$content = $content -replace '\|framed\|','|frame|'
$content = $content -replace '\|framed\]\]','|frame]]'
$content = parseLineByLine $content
$content = handleMailToBlocks $content
$content = handleExternalLink $content
$content = handleTemplates -content $content -pageName $pageName
return $content
}
function handleMailToBlocks($content) {
#make the content in pandoc understandable format
If($mailToOrg) {
$regexStr = '(\[mailto:)(((?!' + $mailToOrg + '|\]|\s).)*)\s+([^\]]*)\]'
$replaceStr = '[mailto:$2' + $mailToOrg + ' $4]'
return $content -replace $regexStr, $replaceStr
}
return $content
}
function handleExternalLink($content) {
$splitArr = $content -split '(\[https:\/\/)(.*?)(\s)(.*?)(\])'
$indexToModify = 2
While($indexToModify -lt $splitArr.Count) {
If(-Not $splitArr[$indexToModify].StartsWith($mediawikiDomain)) {
$splitArr[$indexToModify] = [System.Net.WebUtility]::UrlEncode($splitArr[$indexToModify])
# these are some of the commo characters that url encode did not encode (but decoder decoded correclty)
$splitArr[$indexToModify] = $splitArr[$indexToModify] -replace '\(','%5C%28' # add ecaping
$splitArr[$indexToModify] = $splitArr[$indexToModify] -replace '\)','%5C%29' # add ecaping
$splitArr[$indexToModify] = $splitArr[$indexToModify] -replace '\!','%21'
$splitArr[$indexToModify] = $splitArr[$indexToModify] -replace '\*','%2A'
}
$indexToModify = $indexToModify + 6
}
$newContent = ''
$newContent = $splitArr -join '' # FORM THE COMPLETE URL AGAIN
return $newContent
}
#handles template calls inside any page (could be another template also)
function handleTemplates($content,$pageName) {
# detect a template
$regexArr = $content -split '{{((.|\n)*?)}}'
$newContent = ''
$nextValidIdx = 1
$nextSkipIdx = $nextValidIdx + 1
$currIdx = 0
While($currIdx -lt $regexArr.Length) {
if($currIdx -eq $nextValidIdx) {
# normal pages can be used as tempaltes
# detect that
# BEFORE:
# {{Template:name}}, {{name}} is a temaplte
# {{:name}} is any other page
# {{:Category:name}} is a category used as a tempalte
# AFTER
# {{Template:name}}is definately a temaplte
# {{name}} is any other page
# Category is not handled
if($regexArr[$currIdx].StartsWith($mediaWikiTemplatePrefix, "CurrentCultureIgnoreCase")) {
#removing it to avid case misatches later
$regexArr[$currIdx] = $regexArr[$currIdx].Remove(0, $mediaWikiTemplatePrefix.Length)
#standardize with Template:
$regexArr[$currIdx] = $mediaWikiTemplatePrefix + $regexArr[$currIdx]
} elseif($regexArr[$currIdx].StartsWith(':')){
# this is a page or category name
$regexArr[$currIdx] = $regexArr[$currIdx].TrimStart(':')
} elseif($regexArr[$currIdx].Trim() -ne $mediaWikiPageNameKeyword){ # {{PAGENAME}} is a magic syntax - dont confuse it as a tempalte
# this is also a atemplate - add template to the name
$regexArr[$currIdx] = $mediaWikiTemplatePrefix + $regexArr[$currIdx]
}
# Page section not supported in template syntax
# side effect: #if... is also pruned
$regexArrPipe = $regexArr[$currIdx] -split '\|'
if($regexArrPipe[0].Contains('#')){
$regexArrPipe[0] = ($regexArrPipe[0] -split '#')[0]
}
$regexArr[$currIdx] = $regexArrPipe -join '|'
# add pagename to the syntax if not already there
# also change the temaplte syntax from {{name}} to {{(name)}} - this is to fool pandoc
# otherwise it prunes temapltes from content
if($regexArr[$currIdx].Contains($mediaWikiPageNameKeyword)) {
$regexArr[$currIdx] = '{{(' + $regexArr[$currIdx] + ')}}'
}else {
$regexArr[$currIdx] = '{{(' + $regexArr[$currIdx] + '| ' + $mediaWikiPageNameKeyword + '=' + $pageName + ')}}'
}
$nextValidIdx = $nextValidIdx + 3
}
if($currIdx -eq $nextSkipIdx) {
$nextSkipIdx = $nextSkipIdx + 3
}
else {
$newContent = $newContent + $regexArr[$currIdx]
}
$currIdx++
}
return $newContent
}
# only checks if the curent line is a <space>*<content>
#urls or image text are not considered as code lines
function isLineMediaWikiCodeBlock($line) {
# images and url are not supported inside code blocks
If(-not $line) {
return $false
}
$hasUrlOrImageText = $line.Contains('[[')
$isCodeBlock = $line.StartsWith(' ') -and $line.Trim(' ') -ne '' -and -not $hasUrlOrImageText
return $isCodeBlock
}
#not a foolproof method - avoid using outside of this scope
function isLineHtml($line) {
return $line.Contains('<span') -or $line.Contains('<code') -or $line.Contains('<div') -or $line.Contains('<code')
}
#loophole - if a line contains more than one tag, we check for ending tag for just one
function isLineHtmlCodeBlockStart($line) {
return $line.Contains('<pre') -or $line.Contains('<code') -or $line.Contains('<source') -or $line.Contains('<syntaxhighlight')
}
function parseLineByLine($content) {
#split the content in newline and parse each line
$result = $content -split "`n"
$codeBlockStart = -1
$len = $result.Length
For($ctr = 0; $ctr -lt $len; $ctr++) {
$line = $result[$ctr]
$isLineHtmlCodeBlockStart = isLineHtmlCodeBlockStart $line
$isWikiTableInProgress = $false
If($line -ne $Null) {
if($isLineHtmlCodeBlockStart -or $line.Contains('{| class="wikitable" ')) {
$closingTag = ''
if($line.Contains('<pre')) { $closingTag = '</pre>' }
elseif($line.Contains('<code')) { $closingTag = '</code>' }
elseif($line.Contains('<source')) { $closingTag = '</source>' }
elseif($line.Contains('<syntaxhighlight')) { $closingTag = '</syntaxhighlight>' }
elseif($line.Contains('{| class="wikitable" ')) {
$closingTag = '|}'
$isWikiTableInProgress = $true
}
While($line -ne $Null -and -not $line.Contains($closingTag) -and ($ctr -lt $result.Count)) {
if($isWikiTableInProgress) {
$result[$ctr] = $result[$ctr].TrimStart(' ')
}
$ctr++
$line = $result[$ctr]
}
$codeBlockStart = -1
$isWikiTableInProgress = $false
}
ElseIf(isLineMediaWikiCodeBlock $line) {
if($codeBlockStart -eq -1) {
$codeBlockStart = $ctr
}
}
Else{
if($line.StartsWith(' ')) { # it starts with space but does not qualify for code block
$result[$ctr] = $result[$ctr].TrimStart()
}
$result[$ctr] = $result[$ctr] -replace '<br/>|<br />|<br>', "" #<br> tag causing issues while rendering changing it to \n also has its own set of problems
#enclose everything detected above as code in a asingle code block else pandock createsa anew clock of each line
$idx = $ctr-1
$result = createCodeBlock -lineArr $result -codeBlockStart $codeBlockStart -codeBlockEnd $idx
$codeBlockStart = -1
}
If($line) {
# fixing some common pandoc errors
If($line.StartsWith(':')) {
$result[$ctr] = $result[$ctr].TrimStart(':') #remove ':' as pandoc is unable to understand it
$result[$ctr] = $result[$ctr].TrimStart(' ')
}
If($line.Contains('{{table}}')) {
$result[$ctr] = $result[$ctr] -replace '{{table}}\s*\||{{table}}', ''
}
If($line.Contains('|-|}') ) {
$result[$ctr] = $result[$ctr] -replace '|}', ''
}
}
}
}
$idx = $ctr-1
$result = createCodeBlock -lineArr $result -codeBlockStart $codeBlockStart -codeBlockEnd $idx
$codeBlockStart = -1
$newContent = $result -join "`n"
return $newContent
}
function createCodeBlock($lineArr, $codeBlockStart, $codeBlockEnd) {
If($codeBlockStart -ne -1) {
if($codeBlockStart -eq ($codeBlockEnd)) {
if($lineArr[$codeBlockEnd].Trim(' ') -ne '') {
$lineArr[$codeBlockEnd] = '<pre>' + $lineArr[$codeBlockEnd] + '</pre>'
}
}
Else {
$lineArr[$codeBlockStart] = '<pre>' + $lineArr[$codeBlockStart]
$lineArr[$codeBlockEnd] = $lineArr[$codeBlockEnd] + '</pre>'
}
$codeBlockStart = -1
}
return $lineArr
}
function preProcessTemplateContent($content) {
# remove noinclude tag with its content
$content = $content -replace '<noinclude>((.|\n)*?)<\/noinclude>',''
# remove inludeonlytag but retain content
$content = $content -replace '<includeonly>((.|\n)*?)<\/includeonly>','$1'
# for tempaltes, let links not have pipesymbol
$content = $content -replace '\[\[(.*?)\|(.*?)\]\]','[[$1]]'
# encode parameter syntax so that pandoc ignores it
$content = $content -replace '{{{((.|\n)*?)}}}','%7B%7B%7B$1%7D%7D%7D'
return $content
}
if($pageName) {
preProcessPage -path $pagePath -pageName $pageName
} elseif($templateName) {
preprocessTemplate -path $templatePath -pageName $templateName
}