Skip to content

Instantly share code, notes, and snippets.

@fredrikekre
Created November 20, 2021 20:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fredrikekre/1c2423ee120c28aa37ecb753c9f2fbf2 to your computer and use it in GitHub Desktop.
Save fredrikekre/1c2423ee120c28aa37ecb753c9f2fbf2 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
# MIT License, Copyright (c) 2021 Fredrik Ekre. See end of file for full license.
_subcommands add "franklin"
_subcommands describe "franklin" <<EOF
Usage:
nb franklin <nb selector> file
Description:
Export an nb file to a Franklin-compatible markdown format, see
https://github.com/tlienart/Franklin.jl.
Require julia to be installed and in PATH.
Write to standard out if no output file is given.
EOF
_franklin() {
# First argument is the nb file selector
local _selector="${1:-}"
if [[ -z "${_selector}" ]]; then
_help "franklin"
exit 1
fi
# Extract the file
file="$(_show "${_selector}" --path)"
if [[ ! -f "${file}" ]]; then
echo "File ${file} not found." 1>&2
exit 1
fi
# Second argument is the output file
local dest="${2:--}"
# Make sure julia is installed
julia=${JULIA:-julia}
if ! command -v "${julia}" ---version &> /dev/null; then
echo "Can not find julia installation" 1>&2
exit 1
fi
# Query what we need from the file
tags=$(_list "${file}" --tags)
title=$(_show "${file}" --title)
date=$(_show "${file}" --updated)
${julia} --compile=min --optimize=0 -e "
# Input
nbfile = \"${file}\"
tags = collect(String(chop(x, head=1, tail=0)) for x in eachline(IOBuffer(\"${tags}\")))
title = \"${title}\"
date = match(r\"(\d{4}-\d{2}-\d{2}).*\", \"${date}\")[1]
dest = \"${dest}\"
# Create output
io = IOBuffer()
print(io, \"+++\n\",
\"title = \\\"\", title, \"\\\"\n\",
\"date = \\\"\", date, \"\\\"\n\",
\"tags = \", tags, \"\n\",
\"+++\n\n\"
)
content = read(nbfile, String)
# Remove tags
for t in tags
global content = replace(content, \"#\$t\" => \"\")
end
print(io, content)
seekstart(io)
if dest == \"-\"
write(stdout, io)
else
open(f -> write(f, io), dest, \"w\")
end
"
}
# MIT License
#
# Copyright (c) 2021 Fredrik Ekre
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment