Skip to content

Commit d2d7174

Browse files
committed
Release 0.0.4
1 parent ec9f6c2 commit d2d7174

File tree

6 files changed

+182
-187
lines changed

6 files changed

+182
-187
lines changed

.github/workflows/npm-publish.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ jobs:
1414
uses: actions/setup-node@v2-beta
1515
with:
1616
node-version: '12'
17+
- name: Build dist
18+
run: npm dist
1719
- name: Publish if version has been updated
1820
uses: pascalgn/npm-publish-action@06e0830ea83eea10ed4a62654eeaedafb8bf50fc
1921
with: # All of theses inputs are optional

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
/node_modules/
22
/dist/
3+
/bin/

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@feelpp/asciidoctor-remote-include-processor",
3-
"version": "0.0.3",
3+
"version": "0.0.4",
44
"description": "Asciidoctor remote include processor extension",
55
"main": "dist/node/asciidoctor-remote-include-processor.js",
66
"browser": "dist/browser/asciidoctor-remote-include-processor.js",
Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
const CIRCUMFIX_COMMENT_SUFFIX_RX = / (?:\*[/)]|--%?>)$/
2+
const NEWLINE_RX = /\r\n?|\n/
3+
const TAG_DELIMITER_RX = /[,;]/
4+
const TAG_DIRECTIVE_RX = /\b(?:tag|(end))::(\S+)\[\]$/
5+
const LINES_DOTDOT_RX = /\.\./
6+
7+
function getTags (attrs) {
8+
if ('tag' in attrs) {
9+
const tag = attrs.tag
10+
if (tag && tag !== '!') {
11+
return tag.charAt() === '!' ? new Map().set(tag.substr(1), false) : new Map().set(tag, true)
12+
}
13+
} else if ('tags' in attrs) {
14+
const tags = attrs.tags
15+
if (tags) {
16+
const result = new Map()
17+
let any = false
18+
tags.split(TAG_DELIMITER_RX).forEach((tag) => {
19+
if (tag && tag !== '!') {
20+
any = true
21+
tag.charAt() === '!' ? result.set(tag.substr(1), false) : result.set(tag, true)
22+
}
23+
})
24+
if (any) return result
25+
}
26+
}
27+
}
28+
29+
function applyTagFiltering (contents, tags) {
30+
let selecting, selectingDefault, wildcard
31+
if (tags.has('**')) {
32+
if (tags.has('*')) {
33+
selectingDefault = selecting = tags.get('**')
34+
wildcard = tags.get('*')
35+
tags.delete('*')
36+
} else {
37+
selectingDefault = selecting = wildcard = tags.get('**')
38+
}
39+
tags.delete('**')
40+
} else {
41+
selectingDefault = selecting = !Array.from(tags.values()).includes(true)
42+
if (tags.has('*')) {
43+
wildcard = tags.get('*')
44+
tags.delete('*')
45+
}
46+
}
47+
48+
const lines = []
49+
const tagStack = []
50+
const usedTags = []
51+
let activeTag
52+
let lineNum = 0
53+
let startLineNum
54+
contents.split(NEWLINE_RX).forEach((line) => {
55+
lineNum++
56+
let m
57+
let l = line
58+
if (
59+
(l.endsWith('[]') ||
60+
(~l.indexOf('[] ') &&
61+
(m = l.match(CIRCUMFIX_COMMENT_SUFFIX_RX)) &&
62+
(l = l.substr(0, m.index)).endsWith('[]'))) &&
63+
(m = l.match(TAG_DIRECTIVE_RX))
64+
) {
65+
const thisTag = m[2]
66+
if (m[1]) {
67+
if (thisTag === activeTag) {
68+
tagStack.shift()
69+
;[activeTag, selecting] = tagStack.length ? tagStack[0] : [undefined, selectingDefault]
70+
} else if (tags.has(thisTag)) {
71+
const idx = tagStack.findIndex(([name]) => name === thisTag)
72+
if (~idx) {
73+
tagStack.splice(idx, 1)
74+
// console.warn(`line ${lineNum}: mismatched end tag in include: expected ${activeTag}, found ${thisTag}`)
75+
}
76+
// } else {
77+
// console.warn(`line ${lineNum}: unexpected end tag in include: ${thisTag}`)
78+
// }
79+
}
80+
} else if (tags.has(thisTag)) {
81+
usedTags.push(thisTag)
82+
tagStack.unshift([(activeTag = thisTag), (selecting = tags.get(thisTag))])
83+
} else if (wildcard !== undefined) {
84+
selecting = activeTag && !selecting ? false : wildcard
85+
tagStack.unshift([(activeTag = thisTag), selecting])
86+
}
87+
} else if (selecting) {
88+
if (!startLineNum) startLineNum = lineNum
89+
lines.push(line)
90+
}
91+
})
92+
// Q: use _.difference(Object.keys(tags), usedTags)?
93+
// const missingTags = Object.keys(tags).filter((e) => !usedTags.includes(e))
94+
// if (missingTags.length) {
95+
// console.warn(`tag${missingTags.length > 1 ? 's' : ''} '${missingTags.join(',')}' not found in include`)
96+
// }
97+
return [lines, startLineNum || 1]
98+
}
99+
100+
function getLines (attrs) {
101+
if ('lines' in attrs) {
102+
const lines = attrs.lines
103+
if (lines) {
104+
// console.warn(`have lines` + lines)
105+
const result = [] // new Map()
106+
let any = false
107+
lines.split(TAG_DELIMITER_RX).forEach((line) => {
108+
if (line && line !== '!') {
109+
const tryMultipleLines = line.split(LINES_DOTDOT_RX)
110+
if (tryMultipleLines.length === 1) {
111+
any = true
112+
result.push([tryMultipleLines[0], tryMultipleLines[0]])
113+
} else if (tryMultipleLines.length === 2) {
114+
any = true
115+
result.push([tryMultipleLines[0], tryMultipleLines[1]])
116+
}
117+
}
118+
})
119+
if (any) return result
120+
}
121+
}
122+
}
123+
124+
function applyLineFiltering (contents, linesToInclude) {
125+
const lines = []
126+
let lineNum = 0
127+
let startLineNum
128+
let registerCurrentLine = false
129+
130+
const nLinesPair = linesToInclude.length
131+
let currentLinePair = 0
132+
let startLine = linesToInclude[currentLinePair][0]
133+
let endLine = linesToInclude[currentLinePair][1]
134+
// console.warn(`applyLineFiltering ` + startLine + ' and ' + endLine )
135+
136+
contents.split(NEWLINE_RX).forEach((line) => {
137+
lineNum++
138+
139+
if (!registerCurrentLine) {
140+
if (lineNum === startLine) registerCurrentLine = true
141+
}
142+
143+
if (registerCurrentLine) {
144+
if (!startLineNum) startLineNum = lineNum
145+
lines.push(line)
146+
147+
if (lineNum === endLine) {
148+
registerCurrentLine = false
149+
currentLinePair++
150+
if (currentLinePair >= nLinesPair) return [lines, startLineNum]
151+
else {
152+
startLine = linesToInclude[currentLinePair][0]
153+
endLine = linesToInclude[currentLinePair][1]
154+
}
155+
}
156+
}
157+
})
158+
return [lines, startLineNum || 1]
159+
}
160+
161+
module.exports = function () {
162+
this.includeProcessor(function () {
163+
this.$option('position', '>>')
164+
this.handles((target) => target.startsWith('https://'))
165+
this.process((doc, reader, target, attrs) => {
166+
const contents = require('child_process').execFileSync('curl', ['--silent', '-L', target], { encoding: 'utf8' })
167+
let includeContents = contents
168+
let startLineNum = 1
169+
const tags = getTags(attrs)
170+
const lines = getLines(attrs)
171+
if (tags) [includeContents, startLineNum] = applyTagFiltering(includeContents, tags)
172+
else if (lines) [includeContents, startLineNum] = applyLineFiltering(includeContents, lines)
173+
reader.pushInclude(includeContents, target, target, startLineNum, attrs)
174+
// reader.pushInclude(contents, target, target, 1, attrs)
175+
})
176+
})
177+
}

src/remote-include-processor.js

Lines changed: 0 additions & 185 deletions
This file was deleted.

0 commit comments

Comments
 (0)