Skip to content

Commit 52e88a7

Browse files
committed
Fix the inheritance logic; also remove a lot of cruft
1 parent 1639049 commit 52e88a7

File tree

1 file changed

+18
-38
lines changed

1 file changed

+18
-38
lines changed

src/ntangle.nim

Lines changed: 18 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# NTangle - Basic tangling of Org documents
22
# https://github.com/OrgTangle/ntangle
33

4-
import os, strformat, strutils, tables, terminal, sequtils, options
4+
import os, strformat, strutils, tables, terminal, sequtils
55

66
type
77
DebugVerbosity = enum dvNone, dvLow, dvHigh
@@ -43,7 +43,7 @@ type
4343
UserError = object of Exception
4444
OrgError = object of Exception
4545
HeaderArgs = object
46-
tangle: Option[string]
46+
tangle: string
4747
padline: bool
4848
shebang: string
4949
mkdirp: bool
@@ -85,7 +85,7 @@ proc resetStateVars() =
8585
fileHeaderArgs.clear()
8686
headerArgsDefaults.clear()
8787
# Default tangle header args for all Org levels and languages.
88-
headerArgsDefaults[(0.Natural, "")] = HeaderArgs(tangle : some("no"),
88+
headerArgsDefaults[(0.Natural, "")] = HeaderArgs(tangle : "no",
8989
padline : true,
9090
shebang : "",
9191
mkdirp : false,
@@ -130,34 +130,21 @@ proc parseTangleHeaderProperties(hdrArgs: seq[string], lnum: int, lang: string,
130130
lang
131131
outfile = dir / basename & "." & ext
132132

133-
if fileHeaderArgs.hasKey(outfile):
134-
hArgs = fileHeaderArgs[outfile]
135-
# If :tangle is not specified on a begin_src block, inherit that
136-
# value if possible.
137-
if onBeginSrc and hArgs.tangle.isNone():
138-
if headerArgsDefaults.hasKey((orgLevel, lang)):
139-
hArgs.tangle = headerArgsDefaults[(orgLevel, lang)].tangle
140-
else:
141-
hArgs.tangle = headerArgsDefaults[(orgLevel, "")].tangle
142-
dbg "Line {lnum} - Using fileHeaderArgs[{outfile}], now hArgs = {hArgs}"
143-
elif headerArgsDefaults.hasKey((orgLevel, lang)):
133+
if headerArgsDefaults.hasKey((orgLevel, lang)):
144134
hArgs = headerArgsDefaults[(orgLevel, lang)]
145135
dbg "Line {lnum} - Using Org level {orgLevel} + lang {lang} scope, now hArgs = {hArgs}"
146136
else:
147137
hArgs = headerArgsDefaults[(orgLevel, "")]
148138
dbg "Line {lnum} - Using only Org level {orgLevel} scope, now hArgs = {hArgs}"
149139

150140
# If hArgs already specifies the tangled file path, use that!
151-
if hArgs.tangle.isSome() and
152-
(hArgs.tangle.get() != "yes") and
153-
(hArgs.tangle.get() != "no"):
154-
let
155-
tangledPath = hArgs.tangle.get()
156-
dbg "** Line {lnum} - Old outfile={outfile}, overriding it to {tangledPath}"
157-
if (not tangledPath.startsWith "/"): # if relative path
158-
outfile = dir / tangledPath
141+
if hArgs.tangle != "yes" and
142+
hArgs.tangle != "no":
143+
dbg "** Line {lnum} - Old outfile={outfile}, overriding it to {hArgs.tangle}"
144+
if (not hArgs.tangle.startsWith "/"): # if relative path
145+
outfile = dir / hArgs.tangle
159146
else:
160-
outfile = tangledPath
147+
outfile = hArgs.tangle
161148

162149
for hdrArg in hdrArgs:
163150
let
@@ -167,7 +154,7 @@ proc parseTangleHeaderProperties(hdrArgs: seq[string], lnum: int, lang: string,
167154
dbg "arg={arg}, argval={argval}, onBeginSrc={onBeginSrc}, outfile={outfile}"
168155
case arg
169156
of "tangle":
170-
hArgs.tangle = some(argval)
157+
hArgs.tangle = argval
171158
case argval
172159
of "yes":
173160
discard
@@ -246,34 +233,27 @@ proc parseTangleHeaderProperties(hdrArgs: seq[string], lnum: int, lang: string,
246233
else: # Ignore all other header args
247234
discard
248235

249-
# Update the default HeaderArgs for the current orgLevel+lang
250-
# scope, but only using the header args set using property keyword
251-
# or the drawer property.
252-
if (not onBeginSrc):
253-
dbg "** Line {lnum}: Updating headerArgsDefaults[({orgLevel}, {lang})] to {hArgs}"
254-
headerArgsDefaults[(orgLevel, lang)] = hArgs
236+
# Update the default HeaderArgs for the current orgLevel+lang
237+
# scope, but only using the header args set using property keyword
238+
# or the drawer property.
239+
if (not onBeginSrc):
240+
dbg "** Line {lnum}: Updating headerArgsDefaults[({orgLevel}, {lang})] to {hArgs}"
241+
headerArgsDefaults[(orgLevel, lang)] = hArgs
255242

256243
dbg "[after] Line {lnum} - hArgs = {hArgs}"
257244
if outfile != "":
258245
# Save the updated hArgs to the file-specific HeaderArgs global
259246
# value.
260247
outFileName = outfile
261-
dbg "** Line {lnum}: Updating fileHeaderArgs[{outFileName}] to {hArgs}"
262-
fileHeaderArgs[outFileName] = hArgs
263248

264249
dbg "line={lnum}, onBeginSrc={onBeginSrc}, hArgs.tangle={hArgs.tangle} outfile={outfile} | outFileName={outFileName}"
265250
if onBeginSrc:
266-
if hArgs.tangle.isSome() and
267-
hArgs.tangle.get() != "no":
251+
if hArgs.tangle != "no":
268252
doAssert outFileName != ""
269253
dbg "line {lnum}: buffering enabled for `{outFileName}'"
270254
bufEnabled = true
271255
firstLineSrcBlock = true
272256

273-
# Don't allow further source blocks to inherit the hArgs.tangle
274-
# value set in begin_src header args.
275-
hArgs.tangle = none(string)
276-
277257
dbg "** Line {lnum}: Updating fileHeaderArgs[{outFileName}] to {hArgs}"
278258
fileHeaderArgs[outFileName] = hArgs
279259

0 commit comments

Comments
 (0)