Skip to content

Commit 86bb88e

Browse files
committed
Add support for syntax tree input to remark(1)
This update removes the previously supported `-a, --ast` flag, and renames it to `--tree-out`. In addition, `--tree-in` is now supported to depict that the read input is an already parsed syntax tree in JSON. And, this commit introduces a new `-t, --tree` flag which turns both `--tree-in` and `--tree-out` on, thus specifying a tree input and a tree output. Closes GH-126. Closes GH-156. Reviewed-By: Eugene Sharygin <eush77@gmail.com>
1 parent 610e429 commit 86bb88e

File tree

7 files changed

+72
-19
lines changed

7 files changed

+72
-19
lines changed

doc/remark.1.md

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,13 +132,30 @@ $ remark --no-rc readme.md -oqw
132132
The watch is stopped when `SIGINT` is received (usually done by pressing
133133
`CTRL-C`).
134134

135-
### `-a`, `--ast`
135+
### `-t`, `--tree`
136136

137137
```sh
138-
remark readme.md --ast
138+
remark --tree < input.json > output.json
139139
```
140140

141-
Instead of outputting document the internally used AST is exposed.
141+
Read input as a syntax tree instead of markdown and write output as a
142+
syntax tree instead of markdown.
143+
144+
### `--tree-in`
145+
146+
```sh
147+
remark --tree-in < input.json > output.md
148+
```
149+
150+
Read input as a syntax tree instead of markdown.
151+
152+
### `--tree-out`
153+
154+
```sh
155+
remark --tree-out < input.md > output.json
156+
```
157+
158+
Write output as a syntax tree instead of markdown.
142159

143160
### `-q`, `--quiet`
144161

lib/cli/cli.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -238,11 +238,13 @@ var program = new Command(pack.name)
238238
.option('-u, --use <plugins>', 'use transform plugin(s)', plugins, {})
239239
.option('-e, --ext <extensions>', 'specify extensions', extensions, [])
240240
.option('-w, --watch', 'watch for changes and reprocess', false)
241-
.option('-a, --ast', 'output AST information', false)
242241
.option('-q, --quiet', 'output only warnings and errors', false)
243242
.option('-S, --silent', 'output only errors', false)
244243
.option('-f, --frail', 'exit with 1 on warnings', false)
244+
.option('-t, --tree', 'input and output syntax tree', false)
245245
.option('--file-path <path>', 'specify file path to process as', false)
246+
.option('--tree-out', 'output syntax tree', false)
247+
.option('--tree-in', 'input syntax tree', false)
246248
.option('--no-stdout', 'disable writing to stdout', true)
247249
.option('--no-color', 'disable color in output', false)
248250
.option('--no-rc', 'disable configuration from .remarkrc', false)
@@ -290,7 +292,9 @@ function CLI(argv) {
290292
self.plugins = program.use;
291293
self.color = program.color;
292294
self.out = program.stdout;
293-
self.ast = program.ast;
295+
self.treeIn = program.treeIn || program.tree;
296+
self.treeOut = program.treeOut || program.tree;
297+
self.tree = program.tree || (self.treeIn && self.treeOut);
294298
self.detectRC = program.rc;
295299
self.detectIgnore = program.ignore;
296300
self.quiet = program.quiet;
@@ -308,7 +312,9 @@ function CLI(argv) {
308312
self.plugins = config.plugins;
309313
self.color = config.color;
310314
self.out = config.stdout;
311-
self.ast = config.ast;
315+
self.treeIn = config.treeIn || config.tree;
316+
self.treeOut = config.treeOut || config.tree;
317+
self.tree = config.tree || (config.treeIn && config.treeOut);
312318
self.detectRC = config.detectRC;
313319
self.detectIgnore = config.detectIgnore;
314320
self.quiet = config.quiet;

lib/cli/file-pipeline/parse.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,20 @@ function parse(context) {
4040
return;
4141
}
4242

43+
if (context.fileSet.cli.treeIn) {
44+
debug('Not parsing already parsed document');
45+
46+
try {
47+
file.namespace('mdast').tree = JSON.parse(file.toString());
48+
} catch (err) {
49+
file.fail(err);
50+
}
51+
52+
file.contents = '';
53+
54+
return;
55+
}
56+
4357
debug('Parsing document');
4458

4559
context.processor.parse(file, context.settings);

lib/cli/file-pipeline/stringify.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,14 @@ function stringify(context) {
3939
var file = context.file;
4040
var value;
4141

42-
if (!context.processor || context.ast) {
42+
if (!context.processor) {
4343
debug('Not compiling failed document');
4444
return;
4545
}
4646

4747
debug('Compiling document');
4848

49-
if (cli.ast) {
49+
if (cli.treeOut) {
5050
file.move({
5151
'extension': 'json'
5252
});

man/remark.1

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,15 +125,33 @@ When watching files which would normally regenerate, this behaviour is ignored u
125125
.RE
126126
.P
127127
The watch is stopped when \fBSIGINT\fR is received (usually done by pressing \fBCTRL-C\fR).
128-
.SS "\fB-a\fR, \fB--ast\fR"
128+
.SS "\fB-t\fR, \fB--tree\fR"
129129
.P
130130
.RS 2
131131
.nf
132-
remark readme.md --ast
132+
remark --tree < input.json > output.json
133133
.fi
134134
.RE
135135
.P
136-
Instead of outputting document the internally used AST is exposed.
136+
Read input as a syntax tree instead of markdown and write output as a syntax tree instead of markdown.
137+
.SS "\fB--tree-in\fR"
138+
.P
139+
.RS 2
140+
.nf
141+
remark --tree-in < input.json > output.md
142+
.fi
143+
.RE
144+
.P
145+
Read input as a syntax tree instead of markdown.
146+
.SS "\fB--tree-out\fR"
147+
.P
148+
.RS 2
149+
.nf
150+
remark --tree-out < input.md > output.json
151+
.fi
152+
.RE
153+
.P
154+
Write output as a syntax tree instead of markdown.
137155
.SS "\fB-q\fR, \fB--quiet\fR"
138156
.P
139157
.RS 2

readme.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,11 +189,13 @@ Options:
189189
-u, --use <plugins> use transform plugin(s)
190190
-e, --ext <extensions> specify extensions
191191
-w, --watch watch for changes and reprocess
192-
-a, --ast output AST information
193192
-q, --quiet output only warnings and errors
194193
-S, --silent output only errors
195194
-f, --frail exit with 1 on warnings
195+
-t, --tree input and output syntax tree
196196
--file-path <path> specify file path to process as
197+
--tree-out output syntax tree
198+
--tree-in input syntax tree
197199
--no-stdout disable writing to stdout
198200
--no-color disable color in output
199201
--no-rc disable configuration from .remarkrc

test/cli.sh

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -104,15 +104,11 @@ it "Should ignore herestring when with files (for now)"
104104
assert $? 0
105105

106106
#
107-
# `--ast`.
107+
# `--tree-out`.
108108
#
109109

110-
it "Should accept \`--ast\`"
111-
$COMMAND --ast $markdown > /dev/null 2>&1
112-
assert $? 0
113-
114-
it "Should accept \`-a\`"
115-
$COMMAND -a $markdown > /dev/null 2>&1
110+
it "Should accept \`--tree-out\`"
111+
$COMMAND --tree-out $markdown > /dev/null 2>&1
116112
assert $? 0
117113

118114
#

0 commit comments

Comments
 (0)