Merge branch 'convert'

master
Jonathan Hodgson 4 years ago
commit 69bfb3d395
  1. 2
      Makefile
  2. 60
      inc/to-html
  3. 30
      kb
  4. 18
      templates/main.html

@ -9,6 +9,8 @@ install:
mkdir -p $(DESTDIR)$(PREFIX)/bin mkdir -p $(DESTDIR)$(PREFIX)/bin
sed '/^source/d' inc/* kb > $(DESTDIR)$(PREFIX)/bin/kb sed '/^source/d' inc/* kb > $(DESTDIR)$(PREFIX)/bin/kb
chmod +x $(DESTDIR)$(PREFIX)/bin/kb chmod +x $(DESTDIR)$(PREFIX)/bin/kb
mkdir -p $(DESTDIR)$(PREFIX)/share/kb
cp -r templates $(DESTDIR)$(PREFIX)/share/kb
uninstall: uninstall:

@ -0,0 +1,60 @@
#!/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 "<nav><ul>"
# Loop through each tag
listTags --noheader | while read item; do
local tag="$(echo "$item" | awk -F ' +' '{print $1}')"
local count="$(echo "$item" | awk -F ' +' '{print $2}')"
echo "<li>"
echo "$tag ($count)"
echo "<ul>"
echo "SELECT items.filename,items.title
FROM items LEFT JOIN links ON items.id = links.itemID
LEFT JOIN tags ON links.tagID = tags.id
WHERE tags.name = '$(safeSQL "$tag")'
GROUP BY items.id;" |
sqlite3 "${sqliteFile}" |
awk -F '|' '{sub(/\.md/,".html",$1);print "<li><a href=\"" $1 "\">" $2 "</a></li>"}'
echo "</ul>"
echo "</li>"
done
echo "</ul></nav>"
}
to-html(){
local destination="$1"
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"
}

30
kb

@ -22,7 +22,7 @@ WHITE='\033[1;37m'
NC='\033[0m' NC='\033[0m'
# Provide a variable with the location of this script. # 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 # Source files
# ################################################## # ##################################################
@ -32,6 +32,7 @@ source "inc/tag-management"
source "inc/file-management" source "inc/file-management"
source "inc/yaml" source "inc/yaml"
source "inc/fzf" source "inc/fzf"
source "inc/to-html"
# Utility functions # 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() { mainScript() {
############## Begin Script Here ################### ############## Begin Script Here ###################
@ -319,6 +340,7 @@ mainScript() {
case "${args[0]}" in case "${args[0]}" in
add) addFile "${args[@]:1}"; safeExit ;; add) addFile "${args[@]:1}"; safeExit ;;
convert) convert "${args[@]:1}"; safeExit ;;
deepsearch) deepSearch "${args[@]:1}"; safeExit ;; deepsearch) deepSearch "${args[@]:1}"; safeExit ;;
del|delete) deleteFile "${args[@]:1}"; safeExit ;; del|delete) deleteFile "${args[@]:1}"; safeExit ;;
edit) editFile "${args[@]:1}"; safeExit ;; edit) editFile "${args[@]:1}"; safeExit ;;
@ -383,12 +405,12 @@ usage() {
--asset Adds the file as an asset --asset Adds the file as an asset
--yaml-header Adds a yaml header (default for markdown files) --yaml-header Adds a yaml header (default for markdown files)
--yaml-file Adds a yaml file (default for non-markdown files) --yaml-file Adds a yaml file (default for non-markdown files)
fuzzy [command] Fuzzy select a file fuzzy [default] 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 git [options] Run arbitary git commands on the kb repository
del [title] Delete a file del [title] Delete a file
index Indexes the folder (usful after a clone etc) 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
" "
} }

@ -0,0 +1,18 @@
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="$lang$" xml:lang="en">
<head>
<meta charset="utf-8" />
<meta name="generator" content="KB" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes" />
$if(Tags)$
<meta name="keywords" content="$for(Tags)$$Tags$, $endfor$" />
$endif$
<title>$Title$</title>
</head>
<body>
#TOC#
$body$
</body>
</html>
Loading…
Cancel
Save