marked-conductor 1.0.14 → 1.0.16
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +18 -0
- data/README.md +18 -5
- data/html/Array.html +1 -1
- data/html/Conductor/Command.html +1 -1
- data/html/Conductor/Condition.html +1 -1
- data/html/Conductor/Config.html +1 -1
- data/html/Conductor/Env.html +1 -1
- data/html/Conductor/Script.html +1 -1
- data/html/Conductor.html +2 -2
- data/html/FalseClass.html +1 -1
- data/html/Filter.html +29 -5
- data/html/Hash.html +1 -1
- data/html/Object.html +1 -1
- data/html/README_rdoc.html +1 -1
- data/html/String.html +316 -31
- data/html/TrueClass.html +1 -1
- data/html/created.rid +6 -5
- data/html/index.html +5 -1
- data/html/js/navigation.js.gz +0 -0
- data/html/js/search_index.js +1 -1
- data/html/js/search_index.js.gz +0 -0
- data/html/js/searcher.js.gz +0 -0
- data/html/table_of_contents.html +85 -19
- data/lib/conductor/filter.rb +129 -2
- data/lib/conductor/string.rb +22 -0
- data/lib/conductor/version.rb +1 -1
- data/lib/conductor/yui-compressor.rb +230 -0
- data/lib/conductor.rb +1 -0
- data/src/_README.md +18 -5
- data/test.md +14 -0
- data/test.sh +4 -7
- metadata +4 -2
@@ -0,0 +1,230 @@
|
|
1
|
+
# This is a Ruby port of the YUI CSS compressor
|
2
|
+
# See LICENSE for license information
|
3
|
+
|
4
|
+
module YuiCompressor
|
5
|
+
# Compress CSS rules using a variety of techniques
|
6
|
+
|
7
|
+
class Yui
|
8
|
+
attr_reader :input_size, :output_size
|
9
|
+
|
10
|
+
def initialize
|
11
|
+
@preservedTokens = []
|
12
|
+
@comments = []
|
13
|
+
@input_size = 0
|
14
|
+
@output_size = 0
|
15
|
+
end
|
16
|
+
|
17
|
+
def compress(css, line_length = 0)
|
18
|
+
@input_size = css.length
|
19
|
+
|
20
|
+
css = process_comments_and_strings(css)
|
21
|
+
|
22
|
+
# Normalize all whitespace strings to single spaces. Easier to work with that way.
|
23
|
+
css.gsub!(/\s+/, ' ')
|
24
|
+
|
25
|
+
# Remove the spaces before the things that should not have spaces before them.
|
26
|
+
# But, be careful not to turn "p :link {...}" into "p:link{...}"
|
27
|
+
# Swap out any pseudo-class colons with the token, and then swap back.
|
28
|
+
css.gsub!(/(?:^|\})[^{:]+\s+:+[^{]*\{/) do |match|
|
29
|
+
match.gsub(':', '___PSEUDOCLASSCOLON___')
|
30
|
+
end
|
31
|
+
css.gsub!(/\s+([!{};:>+()\],])/, '\1')
|
32
|
+
css.gsub!(/([!{}:;>+(\[,])\s+/, '\1')
|
33
|
+
css.gsub!('___PSEUDOCLASSCOLON___', ':')
|
34
|
+
|
35
|
+
# special case for IE
|
36
|
+
css.gsub!(/:first-(line|letter)(\{|,)/, ':first-\1 \2')
|
37
|
+
|
38
|
+
# no space after the end of a preserved comment
|
39
|
+
css.gsub!(%r{\*/ }, '*/')
|
40
|
+
|
41
|
+
# If there is a @charset, then only allow one, and push to the top of the file.
|
42
|
+
css.gsub!(/^(.*)(@charset "[^"]*";)/i, '\2\1')
|
43
|
+
css.gsub!(/^(\s*@charset [^;]+;\s*)+/i, '\1')
|
44
|
+
|
45
|
+
# Put the space back in some cases, to support stuff like
|
46
|
+
# @media screen and (-webkit-min-device-pixel-ratio:0){
|
47
|
+
css.gsub!(/\band\(/i, 'and (')
|
48
|
+
|
49
|
+
# remove unnecessary semicolons
|
50
|
+
css.gsub!(/;+\}/, '}')
|
51
|
+
|
52
|
+
# Replace 0(%, em, ex, px, in, cm, mm, pt, pc) with just 0.
|
53
|
+
css.gsub!(/([\s:])([+-]?0)(?:%|em|ex|px|in|cm|mm|pt|pc)/i, '\1\2')
|
54
|
+
|
55
|
+
# Replace 0 0 0 0; with 0.
|
56
|
+
css.gsub!(/:(?:0 )+0(;|\})/, ':0\1')
|
57
|
+
|
58
|
+
# Restore background-position:0 0; if required
|
59
|
+
css.gsub!(/(background-position|transform-origin|webkit-transform-origin|moz-transform-origin|o-transform-origin|ms-transform-origin):0(;|\})/i) {
|
60
|
+
"#{::Regexp.last_match(1).downcase}:0 0#{::Regexp.last_match(2)}" }
|
61
|
+
|
62
|
+
# Replace 0.6 with .6, but only when preceded by : or a space.
|
63
|
+
css.gsub!(/(:|\s)0+\.(\d+)/, '\1.\2')
|
64
|
+
|
65
|
+
# Shorten colors from rgb(51,102,153) to #336699
|
66
|
+
# This makes it more likely that it'll get further compressed in the next step.
|
67
|
+
css.gsub!(/rgb\s*\(\s*([0-9,\s]+)\s*\)/) do |_match|
|
68
|
+
'#' << ::Regexp.last_match(1).scan(/\d+/).map {|n| n.to_i.to_s(16).rjust(2, '0') }.join
|
69
|
+
end
|
70
|
+
|
71
|
+
# Shorten colors from #AABBCC to #ABC. Note that we want to make sure
|
72
|
+
# the color is not preceded by either ", " or =. Indeed, the property
|
73
|
+
# filter: chroma(color="#FFFFFF");
|
74
|
+
# would become
|
75
|
+
# filter: chroma(color="#FFF");
|
76
|
+
# which makes the filter break in IE.
|
77
|
+
css.gsub!(/([^"'=\s])(\s?)\s*#([0-9a-f])\3([0-9a-f])\4([0-9a-f])\5/i, '\1\2#\3\4\5')
|
78
|
+
|
79
|
+
# border: none -> border:0
|
80
|
+
css.gsub!(/(border|border-top|border-right|border-bottom|border-right|outline|background):none(;|\})/i) {
|
81
|
+
"#{::Regexp.last_match(1).downcase}:0#{::Regexp.last_match(2)}" }
|
82
|
+
|
83
|
+
# shorter opacity IE filter
|
84
|
+
css.gsub!(/progid:DXImageTransform\.Microsoft\.Alpha\(Opacity=/i, 'alpha(opacity=')
|
85
|
+
|
86
|
+
# Remove empty rules.
|
87
|
+
css.gsub!(%r{[^\};\{/]+\{\}}, '')
|
88
|
+
|
89
|
+
if line_length > 0
|
90
|
+
# Some source control tools don't like it when files containing lines longer
|
91
|
+
# than, say 8000 characters, are checked in. The linebreak option is used in
|
92
|
+
# that case to split long lines after a specific column.
|
93
|
+
startIndex = 0
|
94
|
+
index = 0
|
95
|
+
length = css.length
|
96
|
+
while index < length
|
97
|
+
index += 1
|
98
|
+
if css[index - 1, 1] === '}' && index - startIndex > line_length
|
99
|
+
css = css.slice(0, index) + "\n" + css.slice(index, length)
|
100
|
+
startIndex = index
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
# Replace multiple semi-colons in a row by a single one
|
106
|
+
# See SF bug #1980989
|
107
|
+
css.gsub!(/;+/, ';')
|
108
|
+
|
109
|
+
# restore preserved comments and strings
|
110
|
+
css = restore_preserved_comments_and_strings(css)
|
111
|
+
|
112
|
+
# top and tail whitespace
|
113
|
+
css.strip!
|
114
|
+
|
115
|
+
@output_size = css.length
|
116
|
+
css
|
117
|
+
end
|
118
|
+
|
119
|
+
def process_comments_and_strings(css_text)
|
120
|
+
css = css_text.clone
|
121
|
+
|
122
|
+
startIndex = 0
|
123
|
+
endIndex = 0
|
124
|
+
i = 0
|
125
|
+
max = 0
|
126
|
+
token = ''
|
127
|
+
totallen = css.length
|
128
|
+
placeholder = ''
|
129
|
+
|
130
|
+
# collect all comment blocks
|
131
|
+
while (startIndex = css.index(%r{/\*}, startIndex))
|
132
|
+
endIndex = css.index(%r{\*/}, startIndex + 2)
|
133
|
+
endIndex = totallen unless endIndex
|
134
|
+
token = css.slice(startIndex + 2..endIndex - 1)
|
135
|
+
@comments.push(token)
|
136
|
+
css = css.slice(0..startIndex + 1).to_s + '___YUICSSMIN_PRESERVE_CANDIDATE_COMMENT_' + (@comments.length - 1).to_s + '___' + css.slice(
|
137
|
+
endIndex, totallen).to_s
|
138
|
+
startIndex += 2
|
139
|
+
end
|
140
|
+
|
141
|
+
# preserve strings so their content doesn't get accidentally minified
|
142
|
+
css.gsub!(/("([^\\"]|\\.|\\)*")|('([^\\']|\\.|\\)*')/) do |match|
|
143
|
+
quote = match[0, 1]
|
144
|
+
string = match.slice(1..-2)
|
145
|
+
|
146
|
+
# maybe the string contains a comment-like substring?
|
147
|
+
# one, maybe more? put'em back then
|
148
|
+
if string =~ /___YUICSSMIN_PRESERVE_CANDIDATE_COMMENT_/
|
149
|
+
@comments.each_index do |index|
|
150
|
+
string.gsub!(/___YUICSSMIN_PRESERVE_CANDIDATE_COMMENT_#{index}___/, @comments[index])
|
151
|
+
end
|
152
|
+
end
|
153
|
+
|
154
|
+
# minify alpha opacity in filter strings
|
155
|
+
string.gsub!(/progid:DXImageTransform\.Microsoft\.Alpha\(Opacity=/i, 'alpha(opacity=')
|
156
|
+
@preservedTokens.push(string)
|
157
|
+
|
158
|
+
quote + '___YUICSSMIN_PRESERVED_TOKEN_' + (@preservedTokens.length - 1).to_s + '___' + quote
|
159
|
+
end
|
160
|
+
|
161
|
+
# used to jump one index in loop
|
162
|
+
ie5_hack = false
|
163
|
+
# strings are safe, now wrestle the comments
|
164
|
+
@comments.each_index do |index|
|
165
|
+
if ie5_hack
|
166
|
+
ie5_hack = false
|
167
|
+
next
|
168
|
+
end
|
169
|
+
|
170
|
+
token = @comments[index]
|
171
|
+
placeholder = '___YUICSSMIN_PRESERVE_CANDIDATE_COMMENT_' + index.to_s + '___'
|
172
|
+
|
173
|
+
# ! in the first position of the comment means preserve
|
174
|
+
# so push to the preserved tokens keeping the !
|
175
|
+
if token[0, 1] === '!'
|
176
|
+
@preservedTokens.push(token)
|
177
|
+
css.gsub!(/#{placeholder}/i, '___YUICSSMIN_PRESERVED_TOKEN_' + (@preservedTokens.length - 1).to_s + '___')
|
178
|
+
next
|
179
|
+
end
|
180
|
+
|
181
|
+
# \ in the last position looks like hack for Mac/IE5
|
182
|
+
# shorten that to /*\*/ and the next one to /**/
|
183
|
+
if token[-1, 1] === '\\'
|
184
|
+
@preservedTokens.push('\\')
|
185
|
+
css.gsub!(/#{placeholder}/, '___YUICSSMIN_PRESERVED_TOKEN_' + (@preservedTokens.length - 1).to_s + '___')
|
186
|
+
# keep the next comment but remove its content
|
187
|
+
@preservedTokens.push('')
|
188
|
+
css.gsub!(/___YUICSSMIN_PRESERVE_CANDIDATE_COMMENT_#{index + 1}___/,
|
189
|
+
'___YUICSSMIN_PRESERVED_TOKEN_' + (@preservedTokens.length - 1).to_s + '___')
|
190
|
+
ie5_hack = true
|
191
|
+
next
|
192
|
+
end
|
193
|
+
|
194
|
+
# keep empty comments after child selectors (IE7 hack)
|
195
|
+
# e.g. html >/**/ body
|
196
|
+
if (token.length === 0) && (startIndex = css.index(/#{placeholder}/))
|
197
|
+
if startIndex > 2
|
198
|
+
if css[startIndex - 3, 1] === '>'
|
199
|
+
@preservedTokens.push('')
|
200
|
+
css.gsub!(/#{placeholder}/, '___YUICSSMIN_PRESERVED_TOKEN_' + (@preservedTokens.length - 1).to_s + '___')
|
201
|
+
end
|
202
|
+
end
|
203
|
+
end
|
204
|
+
|
205
|
+
# in all other cases kill the comment
|
206
|
+
css.gsub!(%r{/\*#{placeholder}\*/}, '')
|
207
|
+
end
|
208
|
+
|
209
|
+
css
|
210
|
+
end
|
211
|
+
|
212
|
+
def restore_preserved_comments_and_strings(clean_css)
|
213
|
+
css = clean_css.clone
|
214
|
+
css_length = css.length
|
215
|
+
@preservedTokens.each_index do |index|
|
216
|
+
# slice these back into place rather than regex, because
|
217
|
+
# complex nested strings cause the replacement to fail
|
218
|
+
placeholder = "___YUICSSMIN_PRESERVED_TOKEN_#{index}___"
|
219
|
+
startIndex = css.index(placeholder, 0)
|
220
|
+
next unless startIndex # skip if nil
|
221
|
+
|
222
|
+
endIndex = startIndex + placeholder.length
|
223
|
+
|
224
|
+
css = css.slice(0..startIndex - 1).to_s + @preservedTokens[index] + css.slice(endIndex, css_length).to_s
|
225
|
+
end
|
226
|
+
|
227
|
+
css
|
228
|
+
end
|
229
|
+
end
|
230
|
+
end
|
data/lib/conductor.rb
CHANGED
data/src/_README.md
CHANGED
@@ -3,7 +3,7 @@
|
|
3
3
|
|
4
4
|
# Marked Conductor
|
5
5
|
|
6
|
-
A "train conductor" for [Marked 2](https://marked2app.com). Conductor can be set up as a Custom Preprocessor or Custom Processor for Marked, and can run different commands and scripts based on conditions in a YAML configuration file, allowing you to have multiple processors that run based on predicates.
|
6
|
+
A "train conductor" for [Marked 2](https://marked2app.com) (Mac only). Conductor can be set up as a Custom Preprocessor or Custom Processor for Marked, and can run different commands and scripts based on conditions in a YAML configuration file, allowing you to have multiple processors that run based on predicates.
|
7
7
|
|
8
8
|
## Installation
|
9
9
|
|
@@ -124,12 +124,16 @@ Conditions can be combined with AND or OR (must be uppercase) and simple parenth
|
|
124
124
|
|
125
125
|
The action can be `script`, `command`, or `filter`.
|
126
126
|
|
127
|
+
#### Scripts
|
128
|
+
|
127
129
|
**Scripts** are located in `~/.config/conductor/scripts/` and should be executable files that take input on STDIN (unless `$file` is specified in the `script` definition). If a script is defined starting with `~` or `/`, that will be interpreted as a full path to an alternate location.
|
128
130
|
|
129
131
|
> Example:
|
130
132
|
>
|
131
133
|
> script: github_pre
|
132
134
|
|
135
|
+
#### Commands
|
136
|
+
|
133
137
|
**Commands** are interpreted as shell commands. If a command exists in the `$PATH`, a full path will automatically be determined, so a command can be as simple as just `pandoc`. Add any arguments needed after the command.
|
134
138
|
|
135
139
|
> Example:
|
@@ -139,21 +143,30 @@ The action can be `script`, `command`, or `filter`.
|
|
139
143
|
|
140
144
|
> Using `$file` as an argument to a script or command will bypass processing of STDIN input, and instead use the value of $MARKED_PATH to read the contents of the specified file.
|
141
145
|
|
146
|
+
#### Filters
|
147
|
+
|
142
148
|
**Filters** are simple actions that can be run on the content without having to write a separate script for it. Available filters are:
|
149
|
+
|
143
150
|
| filter | description |
|
144
151
|
| :---- | :---------- |
|
145
152
|
| `setMeta(key, value)` | adds or updates a meta key, aware of YAML and MMD |
|
146
153
|
| `stripMeta` | strips all metadata (YAML or MMD) from the content |
|
147
|
-
| `
|
148
|
-
| `setStyle(name)` | sets the Marked preview style to a preconfigured Style name
|
154
|
+
| `deleteMeta(key)` | removes a specific key (YAML or MMD) |
|
155
|
+
| `setStyle(name)` | sets the Marked preview style to a preconfigured Style name |
|
149
156
|
| `replace(search, replace)` | performs a (single) search and replace on content |
|
150
157
|
| `replaceAll(search, replace)` | global version of `replaceAll`) |
|
151
158
|
| `insertTitle` | adds a title to the document, either from metadata or filename |
|
152
159
|
| `insertScript(path[,path])` | injects javascript(s) |
|
160
|
+
| `insertTOC(max, after)` | insert TOC (max=max levels, after=start, \*h1, or h2) |
|
161
|
+
| `prepend/appendFile(path)` | insert a file as Markdown at beginning or end of content |
|
162
|
+
| `prepend/appendRaw(path)` | insert a file as raw HTML at beginning or end of content |
|
163
|
+
| `prepend/appendCode(path)` | insert a file as a code block at beginning or end of content |
|
164
|
+
|
165
|
+
For `replace` and `replaceAll`: If *search* is surrounded with forward slashes followed by optional flags (*i* for case-insensitive, *m* to make dot match newlines), e.g. `/contribut(ing)?/i`, it will be interpreted as a regular expression. The *replace* value can include numeric capture groups, e.g. `Follow$2`.
|
153
166
|
|
154
167
|
For `insertScript`, if path is just a filename it will look for a match in `~/.config/conductor/javascript` or `~/.config/conductor/scripts` and turn that into an absolute path if the file is found.
|
155
168
|
|
156
|
-
For
|
169
|
+
For all of the prepend/append file filters, you can store files in `~/.config/conductor/files` and reference them with just a filename. Otherwise a full path will be assumed.
|
157
170
|
|
158
171
|
> Example:
|
159
172
|
>
|
@@ -176,7 +189,7 @@ A script run by Conductor already knows it has the right type of file with the e
|
|
176
189
|
- Config file must be valid YAML. Any value containing colons, brackets, or other special characters should be quoted, e.g. (`condition: "text contains my:text"`)
|
177
190
|
- You can see what condition matched in Marked by opening **Help->Show Custom Processor Log** and checking the STDERR output.
|
178
191
|
- To run [a custom processor for Bear](https://brettterpstra.com/2023/10/08/marked-and-bear/), use the condition `"text contains <!-- source: bear.app -->"`. You might consider running a commonmark CLI with Bear to support more of its syntax.
|
179
|
-
- To run a custom processor for Obsidian, use the condition `tree contains .obsidian`
|
192
|
+
- To run a [custom processor for Obsidian](https://brettterpstra.com/2024/05/16/marked-2-and-obsidian/), use the condition `tree contains .obsidian`
|
180
193
|
|
181
194
|
## Testing
|
182
195
|
|
data/test.md
ADDED
data/test.sh
CHANGED
@@ -1,12 +1,9 @@
|
|
1
1
|
#!/bin/bash
|
2
2
|
|
3
|
-
# ./test.sh PATH
|
3
|
+
# ./test.sh OPTIONS PATH
|
4
|
+
# test.sh -h for options
|
4
5
|
|
5
|
-
OPTIND=1
|
6
|
-
|
7
|
-
# Initialize our own variables:
|
8
|
-
output_file=""
|
9
|
-
verbose=0
|
6
|
+
OPTIND=1
|
10
7
|
|
11
8
|
show_help() {
|
12
9
|
echo "$(basename $0): Shortcut for testing conductor with a given file"
|
@@ -18,7 +15,7 @@ show_help() {
|
|
18
15
|
echo " $0 [-p PHASE] [-o OUTPUT] FILE_PATH"
|
19
16
|
}
|
20
17
|
|
21
|
-
if [[ -
|
18
|
+
if [[ -f bin/conductor ]]; then
|
22
19
|
CMD="./bin/conductor"
|
23
20
|
else
|
24
21
|
CMD="$(which conductor)"
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: marked-conductor
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.16
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Brett Terpstra
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-
|
11
|
+
date: 2024-06-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: awesome_print
|
@@ -275,8 +275,10 @@ files:
|
|
275
275
|
- lib/conductor/script.rb
|
276
276
|
- lib/conductor/string.rb
|
277
277
|
- lib/conductor/version.rb
|
278
|
+
- lib/conductor/yui-compressor.rb
|
278
279
|
- marked-conductor.gemspec
|
279
280
|
- src/_README.md
|
281
|
+
- test.md
|
280
282
|
- test.sh
|
281
283
|
homepage: https://github.com/ttscoff/marked-conductor
|
282
284
|
licenses:
|