From 4c891efdce0b8975387f40f8eda36448c46892da Mon Sep 17 00:00:00 2001 From: Jonathan Hodgson Date: Thu, 31 Dec 2020 00:06:24 +0000 Subject: [PATCH 1/2] Start work on export options I have started an html export. So far a toc is generated --- inc/to-html | 36 ++++++++++++++++++++++ kb | 88 +++++++++++++++++++++++++++++++++-------------------- 2 files changed, 91 insertions(+), 33 deletions(-) create mode 100644 inc/to-html diff --git a/inc/to-html b/inc/to-html new file mode 100644 index 0000000..e01990a --- /dev/null +++ b/inc/to-html @@ -0,0 +1,36 @@ +#!/usr/bin/env bash + +# This file is responsible for generating an html structure of documents +# All md files will be converted to html +# Everything else will be copied exactly + +# The table of contents will be a nested ul structure +# The top level will be the tags, under each tag there will be a list of documents that are tagged with that tag +# This will result in doucuments being listed multiple times, once for each tag they are in +generate-html-toc(){ + echo "" +} + +to-html(){ + local destination="$1" + local toc="$(generate-html-toc)" + echo "$toc" +} diff --git a/kb b/kb index e5942c9..526ca26 100755 --- a/kb +++ b/kb @@ -32,6 +32,7 @@ source "inc/tag-management" source "inc/file-management" source "inc/yaml" source "inc/fzf" +source "inc/to-html" # Utility functions # ################################################## @@ -304,6 +305,26 @@ indexFolder(){ } +convert(){ + local destination + local method + + while [[ $1 = -?* ]]; do + case "$1" in + --to-html) method="to-html"; destination="html" ;; + esac + shift + done + + [ -n "$1" ] && destination="$1" + + [ -z "$method" ] && die "You must specify a conversion method" + [ -z "$destination" ] && die "You must specify a destination" + [ -d "$destination" ] || die "$destination must be a directory" + + "$method" "$destination" +} + mainScript() { ############## Begin Script Here ################### @@ -319,6 +340,7 @@ mainScript() { case "${args[0]}" in add) addFile "${args[@]:1}"; safeExit ;; + convert) convert "${args[@]:1}"; safeExit ;; deepsearch) deepSearch "${args[@]:1}"; safeExit ;; del|delete) deleteFile "${args[@]:1}"; safeExit ;; edit) editFile "${args[@]:1}"; safeExit ;; @@ -354,41 +376,41 @@ usage() { This tool helps manage my personal knowledge base Options: - -q, --quiet Quiet (no output) - -v, --verbose Output more information. (Items echoed to 'verbose') - -d, --debug Runs script in BASH debug mode (set -x) - -h, --help Display this help and exit - --data The knowledgebase data dir - --sqlite The sqlite file (default to /knowledgebase.sqlite3 - --editor The editor to use (default $EDITOR or vim) - --pager The pager to use (default bat or $PAGER or cat) - --nogit Don't run git commands - --version Output version information and exit + -q, --quiet Quiet (no output) + -v, --verbose Output more information. (Items echoed to 'verbose') + -d, --debug Runs script in BASH debug mode (set -x) + -h, --help Display this help and exit + --data The knowledgebase data dir + --sqlite The sqlite file (default to /knowledgebase.sqlite3 + --editor The editor to use (default $EDITOR or vim) + --pager The pager to use (default bat or $PAGER or cat) + --nogit Don't run git commands + --version Output version information and exit Commands: - init Initialise a new knowledgebase - new [options] [title] Create new entry - --filetype Type of file ( default md) - edit [title] Edit a file - list List all files - --noheader Don't include the header - --normal List items of type \"normal\" - list-tags Lists tags with the number of times its used - --noheader Don't include the header - purge-tags Deletes any unused tags - update [] Updates the database and git repo of a changed file - If 2 files are given, it assumes a move - view View a file - add [options] Adds a file - --asset Adds the file as an asset - --yaml-header Adds a yaml header (default for markdown files) - --yaml-file Adds a yaml file (default for non-markdown files) - fuzzy [command] Fuzzy select a file - command is what to do with the selected file - edit or view - git [options] Run arbitary git commands on the kb repository - del [title] Delete a file - index Indexes the folder (usful after a clone etc) + init Initialise a new knowledgebase + new [options] [title] Create new entry + --filetype Type of file ( default md) + edit [title] Edit a file + list List all files + --noheader Don't include the header + --normal List items of type \"normal\" + list-tags Lists tags with the number of times its used + --noheader Don't include the header + purge-tags Deletes any unused tags + update [] Updates the database and git repo of a changed file + If 2 files are given, it assumes a move + view View a file + add [options] Adds a file + --asset Adds the file as an asset + --yaml-header Adds a yaml header (default for markdown files) + --yaml-file Adds a yaml file (default for non-markdown files) + fuzzy [default] Fuzzy select a file + git [options] Run arbitary git commands on the kb repository + del [title] Delete a file + index Indexes the folder (usful after a clone etc) + convert [options] [dest] Converts the notes into a different format and puts it in dest + --to-html Converts to html " } From 7d554d5326ff0a3ea362999c9197d8a53c4b8acf Mon Sep 17 00:00:00 2001 From: Jonathan Hodgson Date: Thu, 31 Dec 2020 09:46:23 +0000 Subject: [PATCH 2/2] Adds the ability to convert to html Issue #14 I still want, at some point, to add to other formats but this is fine for now. The tool uses pandoc to do the conversion from md to html A sample template (without any styling) is provided. This can be overwritten in $HOME/.config/kb/templates/main.html or in the knowledgebase directory under `templates`. --- Makefile | 2 ++ inc/to-html | 28 ++++++++++++++++++++++++++-- kb | 2 +- templates/main.html | 18 ++++++++++++++++++ 4 files changed, 47 insertions(+), 3 deletions(-) create mode 100644 templates/main.html diff --git a/Makefile b/Makefile index 15cddcf..ae34208 100644 --- a/Makefile +++ b/Makefile @@ -9,6 +9,8 @@ install: mkdir -p $(DESTDIR)$(PREFIX)/bin sed '/^source/d' inc/* kb > $(DESTDIR)$(PREFIX)/bin/kb chmod +x $(DESTDIR)$(PREFIX)/bin/kb + mkdir -p $(DESTDIR)$(PREFIX)/share/kb + cp -r templates $(DESTDIR)$(PREFIX)/share/kb uninstall: diff --git a/inc/to-html b/inc/to-html index e01990a..9bf5f1b 100644 --- a/inc/to-html +++ b/inc/to-html @@ -31,6 +31,30 @@ generate-html-toc(){ to-html(){ local destination="$1" - local toc="$(generate-html-toc)" - echo "$toc" + local preTemplate="/usr/local/share/kb/templates/main.html" + local tocFile="$(mktemp)" + local templateFile="$(mktemp)" + generate-html-toc > "$tocFile" + + # If there is a template in the users .local/share directory, use that + [ -e "${XDG_CONFIG_HOME:-$HOME/.config}/kb/templates/main.html" ] && + preTemplate="${XDG_CONFIG_HOME:-$HOME/.config}/kb/templates/main.html" + + # If there is a template in the data directory, use that + [ -e "${dataDir}templates/main.html" ] && + preTemplate="${dataDir}templates/main.html" + + + sed "s#\#TOC\##\${ $tocFile() }#" "$preTemplate" > "$templateFile" + + + find "$dataDir" -name "*.md" | while read file; do + local newFile="${file##*/}" + newFile="${newFile/.md/.html}" + sed -E 's/\.md( *)\)/.html\1\)/' "$file" | + pandoc -f markdown -t html --template "$templateFile" > "$destination/$newFile" + done + + rm "$tocFile" "$templateFile" + } diff --git a/kb b/kb index 526ca26..f5d8519 100755 --- a/kb +++ b/kb @@ -22,7 +22,7 @@ WHITE='\033[1;37m' NC='\033[0m' # Provide a variable with the location of this script. -#scriptPath="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" +scriptPath="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" # Source files # ################################################## diff --git a/templates/main.html b/templates/main.html new file mode 100644 index 0000000..c81b714 --- /dev/null +++ b/templates/main.html @@ -0,0 +1,18 @@ + + + + + + +$if(Tags)$ + +$endif$ +$Title$ + + + +#TOC# + +$body$ + +