-
Notifications
You must be signed in to change notification settings - Fork 4
/
Org.psm1
158 lines (153 loc) · 5.74 KB
/
Org.psm1
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
# SPDX-License-Identifier: GPL-3.0
Function OrgPack1
{
[CmdletBinding()]
param (
# OneUp configuration object
[Parameter(Mandatory)]
[object]
$config
,
# Converted page configuration object
[Parameter(Mandatory)]
[object]
$pageCfg
)
# Markup output formatting using search and replace queries against a string containing the entire markup content.
$markupPack = @(
###############################################################
# CONTENT #
###############################################################
foreach ($attachmentCfg in $pageCfg['insertedAttachments']) {
@{
description = 'Change inserted attachment(s) filename references'
replacements = @(
@{
searchRegex = [regex]::Escape( $attachmentCfg['object'].preferredName )
replacement = "[$( $attachmentCfg['markdownFileName'] )]($( $pageCfg['mediaPathPandoc'] )/$( $attachmentCfg['markdownFileName'] ))"
}
)
}
}
@{
description = 'Replace media (eg: images, attachments) absolute paths with relative paths'
replacements = @(
@{
# eg: 'C:/temp/notes/mynotebook/media/somepage-image1-timestamp.jpg' -> '../media/somepage-image1-timestamp.jpg'
searchRegex = [regex]::Escape("$( $pageCfg['mediaParentPathPandoc'] )/") # Add a trailing front slash
replacement = $pageCfg['levelsPrefix']
}
)
}
@{
description = 'Add heading'
replacements = @(
@{
searchRegex = '^[^\r\n]*'
replacement = & {
$heading = "#+title:$( $pageCfg['object'].name )"
$creationDate = $pageCfg['dateTime']
$creationDay = $creationDate.DayOfWeek.ToString()
if ($config['headerTimestamp']['value'] -eq 1) {
$heading += "`n#+CREATED: $( $creationDate.ToString('<yyyy-MM-dd') ) $( $creationDay.Substring(0, 3) ) $( $creationDate.ToString('HH:mm>') )`n`n"
}
$heading
}
}
)
}
###############################################################
# OPTIONAL #
###############################################################
if ($config['keepEmptyListItems']['value'] -eq 1) {
@{
description = 'Remove empty list items'
replacements = @(
@{
searchRegex = "\n[ ]*- $([char]0x00A0)\n"
replacement = ''
}
@{
searchRegex = "\n[ ]*[0-9]+. $([char]0x00A0)\n"
replacement = ''
}
)
}
}
if ($config['newlineCharacter']['value'] -eq 1) {
@{
description = "Use LF for newlines"
replacements = @(
@{
searchRegex = '\r*\n'
replacement = "`n"
}
)
}
}
if ($config['keepEscape']['value'] -eq 1) {
@{
description = "Remove all '\' characters"
replacements = @(
@{
searchRegex = [regex]::Escape('\')
replacement = ''
}
)
}
}
elseif ($config['keepEscape']['value'] -eq 2) {
@{
description = "Remove all '\' characters except those preceding alphanumeric characters"
replacements = @(
@{
searchRegex = '\\([^A-Za-z0-9])'
replacement = '$1'
}
)
}
}
###############################################################
# CONVERSION ARTIFACTS #
###############################################################
@{
description = 'Remove extra newline after list items and indented paragraphs'
replacements = @(
# Remove double newlines within unordered lists
@{
searchRegex = '(?<=\n[ ]*- .*?)(\n)(?=\n[ ]*-)'
replacement = ''
}
# Remove double newlines within ordered lists
@{
searchRegex = '(?<=\n[ ]*[0-9]+. .*?)(\n)(?=\n[ ]*[0-9]+.)'
replacement = ''
}
# Remove double newlines after indented paragraphs
@{
searchRegex = '(?<=[ ]{2,}([^- 0-9.\n]{2}).*\n)(\n)(\n*)'
replacement = '$3'
}
)
}
@{
description = 'Remove non-breaking spaces from blank lines'
replacements = @(
@{
searchRegex = "(?<=\n)($([char]0x00A0)\n\n)"
replacement = ''
}
)
}
@{
description = 'Remove empty QUOTES'
replacements = @(
@{
searchRegex = '\n#\+BEGIN_QUOTE\n\s*\n#\+END_QUOTE'
replacement = ''
}
)
}
)
$markupPack
}