tildelog/lib/parse_file.sh

54 lines
1.9 KiB
Bash

#!/usr/bin/env bash
# Parse the plain text file into an html file
#
# $1 source file name
# $2 (optional) timestamp for the file
# $3 (optional) destination file name
# note that although timestamp is optional, something must be provided at its
# place if destination file name is provided, i.e:
# parse_file source.txt "" destination.html
declare convert_filename
declare template_tags_line_header
declare prefix_tags
declare global_author
parse_file() {
# Read for the title and check that the filename is ok
title=""
while IFS='' read -r line; do
if [[ -z $title ]]; then
# remove extra <p> and </p> added by markdown
title=$(echo "$line" | sed 's/<\/*p>//g')
if [[ -n $3 ]]; then
filename=$3
else
filename=$title
[[ -n $convert_filename ]] && filename=$(echo "$title" | eval "$convert_filename")
[[ -n $filename ]] || filename=$RANDOM # don't allow empty filenames
filename=$filename.html
# Check for duplicate file names
while [[ -f $filename ]]; do
filename=${filename%.html}$RANDOM.html
done
fi
content=$filename.tmp
# Parse possible tags
elif [[ $line == "<p>$template_tags_line_header"* ]]; then
tags=$(echo "$line" | cut -d ":" -f 2- | sed -e 's/<\/p>//g' -e 's/^ *//' -e 's/ *$//' -e 's/, /,/g')
IFS=, read -r -a array <<<"$tags"
echo -n "<p>$template_tags_line_header " >>"$content"
for item in "${array[@]}"; do
echo -n "<a href='$prefix_tags$item.html'>$item</a>, "
done | sed 's/, $/<\/p>/g' >>"$content"
else
echo "$line" >>"$content"
fi
done <"$1"
# Create the actual html page
create_html_page "$content" "$filename" no "$title" "$2" "$global_author"
rm "$content"
}