skx / sysbox

sysadmin/scripting utilities, distributed as a single binary

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Add embed functionality ..

skx opened this issue · comments

I recently came across this tool:

It allows you to embed code-samples in markdown files, though it must be said it is very flexible and basically you give it an input-file which contains:

MARKER  file|URL start-regexp end-regexp

The line will be replaced by content from the file-path, or the remote URL, between the start/end regexps. Pretty trivial to write this in perl, via the flip-flop operator:

That said it would be a nice addition here.

Something like:

sysbox embed file1.md > file1.out

The biggest, obvious, issue is that if you were to replace in-place, you'd lose the marker(s). So this example:

Hello I am header.

#marker: /etc/passwd /root/ /\n/

Could become:

Hello I am header

root:x:0:0:root:/root:/bin/bash

But then you're stuck! You can't replace the content again, because the edited-in-place file no longer has the marker. You probably don't want that marker inline anyway. Though you could have something like:

<div class="include" start="^root" end="\n" />

Which could remain. But that's getting a bit markdown-specific. I guess the real solution is that your repository would have README.in (master-source) and README.md (generated/expanded output).

Upon reflection this is overkill. It seems that what I really want is:

  • Expand "template" with some magic behaviours.
    • Include a file (complete file)
    • Include lines matching a pattern (grep).
    • Include lines between two regexps.

We already have env-template. How about this:

Hello {{env "USER"}}!

Grep match:

{{grep "/etc/hosts" "\\.vpn"}}

File inline:

{{include "/etc/issue"}}

Between:
{{between "main.go" "func main" "^}"}}

Just an import?

{{between "main.go" "^import" "^\\)"}}

Your $PATH variable has {{len (split (env "PATH") ":")}} entries.

Implement those primitives and it'll work as-is...

(Only obvious issue here is what if we want the lines between two regexps - but not including them. Duplication?)