all repos — hastyscribe @ 58644b71a75763b2084b6464d52fee34044e534a

A professional markdown compiler.

Implement `--no-clobber` to prevent overwrites
Zoom zoomrmc+git@gmail.com
Sun, 17 Sep 2023 05:38:49 +0400
commit

58644b71a75763b2084b6464d52fee34044e534a

parent

550ff4839a02d48a264a789b89f0a0eb16bda0f2

1 files changed, 16 insertions(+), 2 deletions(-)

jump to
M src/hastyscribe.nimsrc/hastyscribe.nim

@@ -45,6 +45,7 @@ watermark*: string

fragment*: bool = false embed*: bool = true iso*: bool = false + noclobber*: bool = false HastyFields* = Table[string, string] HastySnippets* = Table[string, string] HastyMacros* = Table[string, string]

@@ -498,8 +499,10 @@ # Use IDs instead of names for anchors

hs.document = hs.document.replace("<a name=", "<a id=") return hs.document +type ClobberError = object of CatchableError + proc compile*(hs: var HastyScribe, input_file: string) - {.raises: [IOError, ref ValueError, Exception].} = + {.raises: [IOError, ref ValueError, Exception, ClobberError].} = let (dir, name, _) = input_file.splitFile() input: string = input_file.readFile()

@@ -513,9 +516,13 @@ hs.compileFragment(input, dir)

else: hs.compileDocument(input, dir) if output == "-": + # TODO: Notify user if outputting multiple files stdout.write(hs.document) else: - output.writeFile(hs.document) + if fileExists(output) and hs.options.noclobber: + raise newException(ClobberError, output) + else: + output.writeFile(hs.document) ### MAIN

@@ -541,6 +548,7 @@ --noembed If specified, styles and images will not be embedded.

--fragment If specified, an HTML fragment will be generated, without embedding images or stylesheets. --iso Use ISO 8601 date format (e.g., 2000-12-31) in the footer. + --no-clobber | -n Do not overwrite existing files. --help Display the usage information. --version Print version and exit."""

@@ -580,6 +588,9 @@ options.fragment = true

of "iso": noVal() options.iso = true + of "n", "no-clobber", "noclobber": + noVal() + options.noclobber = true of "v", "version": echo pkgVersion quit(0)

@@ -624,6 +635,9 @@ hs.compile(file)

except IOError as e: errorsOccurred.incl errEIO fatal e.msg + continue + except ClobberError as e: + warn "File '" & e.msg & "' exists, not overwriting" continue info "\"$1\" converted successfully" % file if errENOENT in errorsOccurred: quit(2)