forked from jdhitsolutions/PSScriptTools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPrepAdoc.ps1
168 lines (141 loc) · 4.37 KB
/
PrepAdoc.ps1
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
[cmdletbinding(SupportsShouldProcess)]
Param(
[Parameter(Position = 0)]
[ValidateNotNullOrEmpty()]
[ValidateScript( {Test-Path $_})]
[string]$Path = ".",
[Parameter(HelpMessage = "The path to a json file with the processing data for the folder.")]
[ValidateNotNullOrEmpty()]
[ValidateScript( {Test-Path $_})]
[string]$DataPath = ".\adoc-data.json"
)
$prog = @{
Activity = "Prepare ADOC files"
Status = "Initializing"
CurrentOperation = ""
PercentComplete = 0
}
Write-Progress @prog
$ModulePath = Convert-Path $Path
Write-Verbose "[$(Get-Date)] Working from $modulePath"
Write-Verbose "[$(Get-Date)] Importing ruby-related functions"
. C:\scripts\rubydocs.ps1
Write-Verbose "[$(Get-Date)] Importing adoc data from json"
<#
{
"Name" : "PSScriptToolsManual",
"Title": "PSScriptTools Manual",
"CodeTheme": "githubcustom",
"CodeThemePath": "c:\\scripts\\githubcustom.rb",
"Theme": "pdf-theme.yml",
"Files": [
{
"Name": "Intro.md",
"Lines": ""
},
{
"Name": "README.md",
"Lines": "1;13..15;33..-1"
},
{
"Name": "ModuleCommands.md",
"Lines": ""
}
]
}#>
$data = Get-Content $DataPath | ConvertFrom-Json
Write-Verbose "[$(Get-Date)] Get the module version"
$ver = (Import-PowerShellDataFile $ModulePath\*.psd1).moduleversion
Write-Verbose "[$(Get-Date)] Found version $ver"
#define a here-string for the main document in Asciidoctor format
$Main = @"
= $($data.title) v$Ver
:allow-uri-read:
:autofit-option:
:data-uri:
:icons: font
:iconset: fa
:linkattrs:
:rouge-style: $($data.codeTheme)
:source-highlighter: rouge
"@
$files = $data.files
$prog.status = "Processing main files"
$i = 0
foreach ($file in $files) {
$i++
$prog.CurrentOperation = $file.name
$prog.percentComplete = ($i/$files.count)*100
Write-Progress @prog
$item = Join-Path -Path $Modulepath -ChildPath $file.name
Write-Verbose "[$(Get-Date)] Converting $item"
Get-Item -path $item |
ConvertTo-Adoc -Codetheme $data.codeTheme
$adoc = $item -replace "md", "adoc"
$c = Get-Content $adoc
Write-Verbose "[$(Get-Date)] Adjustings links and xrefs"
[regex]$lx = "xref:(?<link>\S+)\.adoc\[(?<display>((\w+[\s-]\w+))+)\]"
$matches = $lx.matches($c)
foreach ($item in $matches) {
$link = $item.groups.where( {$_.name -eq 'link'}).value
$display = $item.groups.where( {$_.name -eq 'display'}).value
#split off the command name from the link to build the
#correct cross-reference in the PDF
$cmdName = $link.split("/")[-1]
$xlink = "$cmdName-Help"
$xref = "<<{0},{1}>>" -f $xlink, $display
#use the method because the value has regex characters
$c = $c.replace($item.value, $xref)
}
Write-Verbose "[$(Get-Date)] Updating $adoc"
$c | Out-File -FilePath $adoc -Encoding utf8
Write-Verbose "[$(Get-Date)] Adding to Main"
#include line subsets if defined
if ($file.lines) {
$trim = "lines=$($file.lines)"
}
else {
$trim = ""
}
$main += @"
include::$($adoc)[$trim]
"@
}
Write-Verbose "[$(Get-Date)] Creating adoc help files. You may need to press Enter to force the process to continue."
#create the adoc files and then edit them.
$prog.Status = "Converting markdown to adoc files"
$prog.CurrentOperation =
$i = 0
$helpMD = Get-ChildItem $ModulePath\docs\*-*.md
$helpmd | ForEach-Object {
$i++
$prog.CurrentOperation = $_.FullName
$prog.percentComplete = ($i/$helpmd.count)*100
Write-Progress @prog
ConvertTo-Adoc -Fullname $_.fullname -passthru -theme $data.codeTheme | Edit-Adoc
}
$prog.status = "Building main document"
Write-Verbose "[$(Get-Date)] Add include links to the main document"
$adocs = Get-ChildItem $ModulePath\docs\*.adoc -OutVariable ov | Sort-Object -property Name
$i = 0
ForEach ($item in $adocs) {
$i++
$prog.CurrentOperation = $item.name
$prog.percentComplete = ($i/$adocs.count)*100
Write-Progress @prog
Write-Verbose "[$(Get-Date)] Adding $($item.name)"
$main += @"
include::docs\$($item.name)[]
"@
}
$out = Join-Path -path $ModulePath -child "$($data.name).adoc"
$prog.status = "Finalizing"
$prog.CurrentOperation = $out
Write-Progress @prog
Write-Verbose "[$(Get-Date)] Saving document to $out"
$main | Out-File -FilePath $out -Encoding utf8
Write-Progress -Activity $prog.Activity -Completed
Write-Host
$msg = "Edit and review the adoc files and then run makepdf.ps1"
Write-Host $msg -ForegroundColor Cyan
Write-Verbose "[$(Get-Date)] Finished."