Skip to content

Commit ad63da5

Browse files
committed
Don't mind extra spaces in-between header-args lines
1 parent 558286f commit ad63da5

File tree

2 files changed

+19
-9
lines changed

2 files changed

+19
-9
lines changed

src/ntangle.nim

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ proc updateHeaderArgsDefault() =
315315
## Update the default header args for the current orgLevel scope.
316316
# Switch to sibling heading. Example: from "** Heading 4.2" to "** Heading 4.3".
317317
if prevOrgLevel == orgLevel.int:
318-
doAssert orgLevel != 0
318+
assert orgLevel != 0
319319
for i in countDown(orgLevel-1, 0):
320320
if headerArgsDefaults.hasKey((i.Natural, "")):
321321
headerArgsDefaults[(orgLevel, "")] = headerArgsDefaults[(i.Natural, "")]
@@ -334,7 +334,7 @@ proc updateHeaderArgsDefault() =
334334
discard
335335
prevOrgLevel = orgLevel
336336

337-
proc getHeaderArgs(s: string): LangAndArgs =
337+
proc getHeaderArgs(line: string, lnum: int): LangAndArgs =
338338
## Get well-formatted header args.
339339
##
340340
## Examples:
@@ -350,17 +350,24 @@ proc getHeaderArgs(s: string): LangAndArgs =
350350
## The ``lang`` field will be an empty string or a language string
351351
## like ``"nim"``.
352352
let
353-
spaceSepParts = s.strip.split(" ")
353+
spaceSepPartsRaw = line.strip.split(" ")
354354
var
355+
spaceSepParts: seq[string]
355356
haType: HeaderArgType = haNone
356357
headerArgsRaw: seq[string] = @[]
357358
headerArgs: seq[string] = @[]
358359
headerArgPair: string
359360
lang: string
361+
for p in spaceSepPartsRaw:
362+
if p.strip() != "":
363+
spaceSepParts.add(p)
360364
dbg "spaceSepParts: {spaceSepParts}", dvHigh
361365
if spaceSepParts.len >= 3 and
362-
spaceSepParts[0].toLowerAscii() == "#+property:":
363-
doAssert spaceSepParts[2][0] == ':'
366+
spaceSepParts[0].toLowerAscii() == "#+property:" and
367+
spaceSepParts[1].toLowerAscii().startsWith("header-args"):
368+
doAssert spaceSepParts[2][0] == ':',
369+
fmt"{orgFile}:{lnum} :: {line}" & "\n" &
370+
" : The first switch in 'header-args' property must be a key with ':' prefix."
364371
headerArgsRaw = spaceSepParts[2 .. spaceSepParts.high]
365372
let
366373
kwdParts = spaceSepParts[1].split(":")
@@ -370,7 +377,9 @@ proc getHeaderArgs(s: string): LangAndArgs =
370377
# ":header-args:", ":header-args+:", ":header-args:nim:"
371378
elif spaceSepParts.len >= 3 and
372379
spaceSepParts[0].toLowerAscii().startsWith(":header-args"):
373-
doAssert spaceSepParts[1][0] == ':'
380+
doAssert spaceSepParts[1][0] == ':',
381+
fmt"{orgFile}:{lnum} :: {line}" & "\n" &
382+
" : The first switch in 'header-args' drawer property must be a key with ':' prefix."
374383
headerArgsRaw = spaceSepParts[1 .. spaceSepParts.high]
375384
let
376385
kwdParts = spaceSepParts[0].split(":")
@@ -414,7 +423,7 @@ proc lineAction(line: string, lnum: int) =
414423
dbg "orgLevel = {orgLevel}"
415424
updateHeaderArgsDefault()
416425
let
417-
(haType, haLang, haArgs) = line.getHeaderArgs()
426+
(haType, haLang, haArgs) = getHeaderArgs(line, lnum)
418427
dbg "[line {lnum}] {line}", dvHigh
419428
if haType != haNone:
420429
dbg "getHeaderArgs: line {lnum}:: {haType}, {haLang}, {haArgs}"
@@ -441,7 +450,7 @@ proc lineAction(line: string, lnum: int) =
441450
blockIndent = (line.len - line.strip(trailing=false).len)
442451

443452
try:
444-
doAssert outFileName != ""
453+
assert outFileName != ""
445454
if firstLineSrcBlock and fileHeaderArgs[outFileName].padline:
446455
fileData[outFileName].add("\n")
447456
fileData[outFileName].add(lineAdjust(line, blockIndent))

tests/tangle_no_yes/tangle_no_yes.org

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
echo "this block won't get tangled"
33
#+end_src
44

5-
#+begin_src nim :tangle yes
5+
Below has extra spaces before ":tangle". But that should not matter.
6+
#+begin_src nim :tangle yes
67
echo "this will be tangled to tangle_no_yes.nim"
78
#+end_src
89

0 commit comments

Comments
 (0)