Skip to content

Commit

Permalink
preserve indentation if current or previous line has hard wrap contin…
Browse files Browse the repository at this point in the history
…uation; remove trailing hard wrap continuation
  • Loading branch information
mojavelinux committed Jul 24, 2023
1 parent d98cd7e commit b76727d
Show file tree
Hide file tree
Showing 13 changed files with 88 additions and 24 deletions.
45 changes: 29 additions & 16 deletions grammar/asciidoc-block.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -230,20 +230,27 @@ attribute_name = $([a-zA-Z0-9_] [a-zA-Z0-9_-]*)
//attribute_value = space lines:($(!lf .)*)|1.., &{ let idx = offset(), count = 0; while (input[--idx] === '\\') count++; return !!(count % 2) } lf (&space / !(attribute_entry_head / eol / list_continuation / any_block_delimiter_line / block_attribute_line))|
//{
// const lastLineIdx = lines.length - 1
// if (!lastLineIdx) {
// const line0 = lines[0]
// if (!line0) return line0
// return line0[0] === ' ' ? line0.trimStart() : line0
// if (lines.length === 1) {
// const line = lines[0]
// if (!line) return line
// const len = line.length
// if (len > 2 && line[len - 1] === '\\' && line[len - 2] === ' ' && line[len - 3] === ' ') return line.slice(0, len - 3)
// return line[0] === ' ' ? line.trimStart() : line
// }
// let hardWrapNext = false
// const lastLineIdx = lines.length - 1
// return lines.reduce((buf, line, lineIdx) => {
// const hardWrap = hardWrapNext
// if (hardWrap) buf = buf.slice(0, -2) + '\n'
// if (hardWrap) buf += '\n'
// hardWrapNext = false
// let len = line.length
// if (lineIdx < lastLineIdx) {
// hardWrapNext = false
// if (!(line = line.slice(0, -1))) return buf
// if (line.length > 1 && line[line.length - 2] === ' ' && line[line.length - 1] === ' ') hardWrapNext = true
// if (!(line = line.substring(0, --len))) return buf
// if (len > 1 && line[len - 2] === ' ' && line[len - 1] === ' ' && (hardWrapNext = true)) {
// return buf + line.substring(0, len - 2)
// }
// } else if (len > 2 && line[len - 1] === '\\' && line[len - 2] === ' ' && line[len - 3] === ' ') {
// return buf + line.substring(0, len - 3)
// }
// return buf + (hardWrap || line[0] !== ' ' ? line : line.trimStart())
// }, '')
Expand All @@ -252,18 +259,24 @@ attribute_name = $([a-zA-Z0-9_] [a-zA-Z0-9_-]*)
attribute_value = space lines:($(!('\\' / lf) . / '\\' !lf .)*)|1.., '\\' lf (&space / !(attribute_entry_head / eol / list_continuation / any_block_delimiter_line / block_attribute_line))| trailer:('\\' / '')
{
if (lines.length === 1) {
const line0 = lines[0]
if (!line0) return trailer
return (line0[0] === ' ' ? line0.trimStart() : line0) + trailer
const line = lines[0]
if (!line) return trailer
if (!trailer) return line[0] === ' ' ? line.trimStart() : line
const len = line.length
if (len > 1 && line[len - 1] === ' ' && line[len - 2] === ' ') return line.substring(0, len - 2)
return (line[0] === ' ' ? line.trimStart() : line) + trailer
}
let hardWrapNext = false
return lines.reduce((buf, line) => {
const lastLineIdx = lines.length - 1
return lines.reduce((buf, line, lineIdx) => {
const hardWrap = hardWrapNext
if (hardWrap) buf += '\n'
hardWrapNext = false
if (hardWrap) buf = buf.slice(0, -2) + '\n'
if (!line) return buf
if (line.length > 1 && line[line.length - 2] === ' ' && line[line.length - 1] === ' ') hardWrapNext = true
return buf + (hardWrap || line[0] !== ' ' ? line : line.trimStart())
const len = line.length
return len > 1 && line[len - 1] === ' ' && line[len - 2] === ' ' && (hardWrapNext = true)
? buf + line.substring(0, len - 2) + (lineIdx === lastLineIdx ? (trailer = '') : '')
: buf + (hardWrap || line[0] !== ' ' ? line : line.trimStart())
}, '') + trailer
}
Expand Down
2 changes: 1 addition & 1 deletion test/tests/block/attributes/backslash-only-input.adoc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
:bs: \
:esc: \
6 changes: 3 additions & 3 deletions test/tests/block/attributes/backslash-only-output.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
"type": "block",
"header": {
"attributes": {
"bs": { "value": "\\", "location": [{ "line": 1, "col": 1 }, { "line": 1, "col": 6 }] }
"esc": { "value": "\\", "location": [{ "line": 1, "col": 1 }, { "line": 1, "col": 7 }] }
},
"location": [{ "line": 1, "col": 1 }, { "line": 1, "col": 6 }]
"location": [{ "line": 1, "col": 1 }, { "line": 1, "col": 7 }]
},
"location": [{ "line": 1, "col": 1 }, { "line": 1, "col": 6 }]
"location": [{ "line": 1, "col": 1 }, { "line": 1, "col": 7 }]
}
3 changes: 3 additions & 0 deletions test/tests/block/attributes/dangling-hard-wrap-input.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
:source: \
foo \
bar \
11 changes: 11 additions & 0 deletions test/tests/block/attributes/dangling-hard-wrap-output.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "document",
"type": "block",
"header": {
"attributes": {
"source": { "value": "foo\nbar", "location": [{ "line": 1, "col": 1 }, { "line": 3, "col": 6 }] }
},
"location": [{ "line": 1, "col": 1 }, { "line": 3, "col": 6 }]
},
"location": [{ "line": 1, "col": 1 }, { "line": 3, "col": 6 }]
}
2 changes: 1 addition & 1 deletion test/tests/block/attributes/hard-wrap-indented-input.adoc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
:filetree: article.adoc \
assets/ \
example.rb \
image.png
image.png \
6 changes: 3 additions & 3 deletions test/tests/block/attributes/hard-wrap-indented-output.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
"type": "block",
"header": {
"attributes": {
"filetree": { "value": "article.adoc\nassets/\n example.rb\n image.png", "location": [{ "line": 1, "col": 1 }, { "line": 4, "col": 11 }] }
"filetree": { "value": "article.adoc\nassets/\n example.rb\n image.png", "location": [{ "line": 1, "col": 1 }, { "line": 4, "col": 14 }] }
},
"location": [{ "line": 1, "col": 1 }, { "line": 4, "col": 11 }]
"location": [{ "line": 1, "col": 1 }, { "line": 4, "col": 14 }]
},
"location": [{ "line": 1, "col": 1 }, { "line": 4, "col": 11 }]
"location": [{ "line": 1, "col": 1 }, { "line": 4, "col": 14 }]
}
1 change: 1 addition & 0 deletions test/tests/block/attributes/indented-hard-wrap-input.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
:source: foo \
11 changes: 11 additions & 0 deletions test/tests/block/attributes/indented-hard-wrap-output.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "document",
"type": "block",
"header": {
"attributes": {
"source": { "value": " foo", "location": [{ "line": 1, "col": 1 }, { "line": 1, "col": 17 }] }
},
"location": [{ "line": 1, "col": 1 }, { "line": 1, "col": 17 }]
},
"location": [{ "line": 1, "col": 1 }, { "line": 1, "col": 17 }]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
:source: indented \
11 changes: 11 additions & 0 deletions test/tests/block/attributes/indented-single-line-output.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "document",
"type": "block",
"header": {
"attributes": {
"source": { "value": " indented", "location": [{ "line": 1, "col": 1 }, { "line": 1, "col": 22 }] }
},
"location": [{ "line": 1, "col": 1 }, { "line": 1, "col": 22 }]
},
"location": [{ "line": 1, "col": 1 }, { "line": 1, "col": 22 }]
}
2 changes: 2 additions & 0 deletions test/tests/block/attributes/trailing-hard-wrap-input.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
:source: foo \
\
11 changes: 11 additions & 0 deletions test/tests/block/attributes/trailing-hard-wrap-output.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "document",
"type": "block",
"header": {
"attributes": {
"source": { "value": "foo\n", "location": [{ "line": 1, "col": 1 }, { "line": 2, "col": 3 }] }
},
"location": [{ "line": 1, "col": 1 }, { "line": 2, "col": 3 }]
},
"location": [{ "line": 1, "col": 1 }, { "line": 2, "col": 3 }]
}

0 comments on commit b76727d

Please sign in to comment.