From 0f0a355a931c7501cdabe2d0efe59e00dd584a00 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20B=C3=B6ving?= Date: Sun, 12 Dec 2021 13:20:03 +0100 Subject: [PATCH] feat: include_str macro for static file inclusion --- DocGen4/IncludeStr.lean | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 DocGen4/IncludeStr.lean diff --git a/DocGen4/IncludeStr.lean b/DocGen4/IncludeStr.lean new file mode 100644 index 0000000..335365a --- /dev/null +++ b/DocGen4/IncludeStr.lean @@ -0,0 +1,21 @@ +import Lean + +namespace DocGen4 + +open Lean System IO Lean.Elab.Term + +syntax (name := includeStr) "include_str" str : term + +@[termElab includeStr] def includeStrImpl : TermElab := λ stx expectedType? => do + let str := stx[1].isStrLit?.get! + let path := FilePath.mk str + if ←path.pathExists then + if ←path.isDir then + throwError s!"{str} is a directory" + else + let content ← FS.readFile path + return mkStrLit content + else + throwError s!"\"{str}\" does not exist as a file" + +end DocGen4