Skip to content

Commit

Permalink
Allow for nodes to *decrement* the line count as well, if they print …
Browse files Browse the repository at this point in the history
…to more lines than they occupied in the source.
  • Loading branch information
tabatkins committed Nov 15, 2023
1 parent d967754 commit 2c97732
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 10 deletions.
20 changes: 14 additions & 6 deletions bikeshed/InputSource.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,27 @@ def lines(self) -> list[line.Line]:
offset = 0
for i, text in enumerate(self.rawLines, 1):
lineNo = i + offset
# The early HTML parser runs before Markdown,
# and in some cases removes linebreaks that were present
# in the source. When properly invoked, it inserts
# a special PUA char for each of these omitted linebreaks,
# so I can remove them here and properly increment the
# line number.
# The early HTML parser can change how nodes print,
# so they occupy a different number of lines than they
# had in the source. Markdown parser needs to know
# the correct source lines, tho, so when this happens,
# the nodes will insert special PUA chars to indicate that.
# I can remove them here and properly adjust the line number.
# Current known causes of this:
# * line-ending -- turned into em dashes
# * multi-line start tags
# * multi-line markdown code spans;
# - the text loses its newlines
# - the original text goes into an attribute on the start
# tag now
ilcc = constants.incrementLineCountChar
dlcc = constants.decrementLineCountChar
if ilcc in text:
offset += text.count(ilcc)
text = text.replace(ilcc, "")
if dlcc in text:
offset -= text.count(dlcc)
text = text.replace(dlcc, "")

ret.append(line.Line(lineNo, text))

Expand Down
1 change: 1 addition & 0 deletions bikeshed/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@
macroStartChar = "\uebbb"
macroEndChar = "\uebbc"
incrementLineCountChar = "\uebbd"
decrementLineCountChar = "\uebbf"
bsComment = "<!--\uebbe-->"
12 changes: 8 additions & 4 deletions bikeshed/h/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,7 @@ def initialDocumentParse(text: str, config: ParseConfig, startLine: int = 1) ->
def strFromNodes(nodes: t.Iterable[ParserNode], withIlcc: bool = False) -> str:
strs = []
ilcc = constants.incrementLineCountChar
dlcc = constants.decrementLineCountChar
for node in nodes:
if isinstance(node, Comment):
# Serialize comments as a standardized, recognizable sequence
Expand All @@ -287,10 +288,13 @@ def strFromNodes(nodes: t.Iterable[ParserNode], withIlcc: bool = False) -> str:
continue
s = str(node)
if withIlcc:
numLines = s.count("\n")
diffLineNo = node.endLine - node.line
if diffLineNo > numLines:
s += ilcc * (diffLineNo - numLines)
outputExtraLines = s.count("\n")
sourceExtraLines = node.endLine - node.line
diff = sourceExtraLines - outputExtraLines
if diff > 0:
s += ilcc * diff
elif diff < 0:
s += dlcc * -diff
strs.append(s)
return "".join(strs)

Expand Down

0 comments on commit 2c97732

Please sign in to comment.