forked from HPR/hpr-tools
		
	
		
			
	
	
		
			190 lines
		
	
	
		
			4.5 KiB
		
	
	
	
		
			Plaintext
		
	
	
	
	
	
		
		
			
		
	
	
			190 lines
		
	
	
		
			4.5 KiB
		
	
	
	
		
			Plaintext
		
	
	
	
	
	
|   | #!/bin/bash - | ||
|  | #=============================================================================== | ||
|  | # | ||
|  | #         FILE: do_pandoc_assets | ||
|  | # | ||
|  | #        USAGE: ./do_pandoc_assets shownumber | ||
|  | # | ||
|  | #  DESCRIPTION: Runs Pandoc on any assets that need it. The assets are in the | ||
|  | #               directory ~/HPR/Show_Submission/shownotes/hpr1234/ and the | ||
|  | #               result is written to the 'uploads' directory to go to the HPR | ||
|  | #               server. | ||
|  | # | ||
|  | #      OPTIONS: --- | ||
|  | # REQUIREMENTS: --- | ||
|  | #         BUGS: --- | ||
|  | #        NOTES: --- | ||
|  | #       AUTHOR: Dave Morriss (djm), Dave.Morriss@gmail.com | ||
|  | #      VERSION: 0.0.1 | ||
|  | #      CREATED: 2024-08-14 18:54:21 | ||
|  | #     REVISION: 2024-08-14 23:15:48 | ||
|  | # | ||
|  | #=============================================================================== | ||
|  | 
 | ||
|  | set -o nounset                              # Treat unset variables as an error | ||
|  | 
 | ||
|  | SCRIPT=${0##*/} | ||
|  | # DIR=${0%/*} | ||
|  | 
 | ||
|  | VERSION='0.0.1' | ||
|  | 
 | ||
|  | STDOUT="/dev/fd/2" | ||
|  | 
 | ||
|  | # | ||
|  | # Load library functions | ||
|  | # | ||
|  | LIB="$HOME/HPR/function_lib.sh" | ||
|  | [ -e "$LIB" ] || { echo "$SCRIPT: Unable to source functions"; exit 1; } | ||
|  | # shellcheck source=/home/cendjm/HPR/function_lib.sh | ||
|  | source "$LIB" | ||
|  | 
 | ||
|  | # | ||
|  | # Colour codes | ||
|  | # | ||
|  | define_colours | ||
|  | 
 | ||
|  | # {{{ Functions: -- _usage -- | ||
|  | #===  FUNCTION  ================================================================ | ||
|  | #         NAME: _usage | ||
|  | #  DESCRIPTION: Report usage | ||
|  | #   PARAMETERS: None | ||
|  | #      RETURNS: Nothing | ||
|  | #=============================================================================== | ||
|  | _usage () { | ||
|  |     cat >$STDOUT <<-endusage | ||
|  | Usage: ./${SCRIPT} [-h] [-d] shownumber | ||
|  | 
 | ||
|  | Version: $VERSION | ||
|  | 
 | ||
|  | Runs Pandoc against assets for a particular show, catering for the case when | ||
|  | a host sends in external notes (referenced from the main notes) in Markdown | ||
|  | format rather than HTML. | ||
|  | 
 | ||
|  | For the moment the only format catered for is Markdown, in files with the | ||
|  | suffix 'md' or 'mkd' | ||
|  | 
 | ||
|  | Options: | ||
|  |   -h                    Print this help | ||
|  |   -d                    Select dry run mode | ||
|  | 
 | ||
|  | Arguments: | ||
|  |     shownumber | ||
|  | 
 | ||
|  | Examples | ||
|  |   ./${SCRIPT} -h | ||
|  |   ./${SCRIPT} -d 2240 | ||
|  | 
 | ||
|  | endusage | ||
|  |     exit | ||
|  | } | ||
|  | # }}} | ||
|  | 
 | ||
|  | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|  | 
 | ||
|  | # | ||
|  | # Sanity checks | ||
|  | # | ||
|  | JQ=$(command -v jq) | ||
|  | [ -n "$JQ" ] || { echo "Program 'jq' was not found"; exit 1; } | ||
|  | 
 | ||
|  | # | ||
|  | # Process options first | ||
|  | # | ||
|  | while getopts :dDh opt | ||
|  | do | ||
|  |     case "${opt}" in | ||
|  |         d) DRYRUN=1;; | ||
|  |         h) _usage;; | ||
|  |         ?) echo "$SCRIPT: Invalid option; aborting"; exit 1;; | ||
|  |     esac | ||
|  | done | ||
|  | shift $((OPTIND - 1)) | ||
|  | 
 | ||
|  | # | ||
|  | # Default options if not provided | ||
|  | # | ||
|  | DRYRUN=${DRYRUN:-0} | ||
|  | 
 | ||
|  | # | ||
|  | # Check there's an argument after removing any options. Abort if not | ||
|  | # | ||
|  | if [[ $# -ne 1 ]]; then | ||
|  |     _usage | ||
|  | fi | ||
|  | 
 | ||
|  | # | ||
|  | # Make the explicit show id, catering for leading zeroes (belt & braces) | ||
|  | # | ||
|  | # TODO: cater for 'hpr1234' as well as a plain number | ||
|  | printf -v SHOWID 'hpr%04d' "$1" | ||
|  | 
 | ||
|  | # | ||
|  | # Paths to files | ||
|  | # | ||
|  | # ------------------------------------------------------------------------------ | ||
|  | # Main directory | ||
|  | BASENAME="$HOME/HPR/Show_Submission" | ||
|  | 
 | ||
|  | # JSON to YAML Perl script | ||
|  | # J2Y="$BASENAME/author_title.pl" | ||
|  | # [ -e "$J2Y" ] || { echo "Program '$J2Y' was not found"; exit 1; } | ||
|  | 
 | ||
|  | # The notes for all shows are here | ||
|  | SHOWNOTES="$BASENAME/shownotes" | ||
|  | 
 | ||
|  | # Notes for this show are here | ||
|  | SHOWDIR="$SHOWNOTES/$SHOWID" | ||
|  | 
 | ||
|  | # Assets which need Pandoc are here | ||
|  | ASSETDIR="$SHOWDIR/uploads" | ||
|  | 
 | ||
|  | # The incoming JSON from the upload form | ||
|  | JSONFILE="$SHOWDIR/shownotes.json" | ||
|  | 
 | ||
|  | # | ||
|  | # Check there are Markdown files in the asset directory and collect their | ||
|  | # names | ||
|  | # | ||
|  | declare -a MARKDOWN | ||
|  | mapfile -t MARKDOWN < <(find "$ASSETDIR" -type f -regextype posix-extended -regex '.*\.mk?d') | ||
|  | 
 | ||
|  | # | ||
|  | # Nothing found? | ||
|  | # | ||
|  | if [[ ${#MARKDOWN[@]} -eq 0 ]]; then | ||
|  |     coloured 'red' "No Markdown files found" | ||
|  |     exit 1 | ||
|  | fi | ||
|  | 
 | ||
|  | # coloured 'cyan' "Markdown files found:" | ||
|  | # printf '%s\n' "${MARKDOWN[@]}" | ||
|  | 
 | ||
|  | # | ||
|  | # Process whatever markup we have, only catering for Markdown at present | ||
|  | # | ||
|  | for file in "${MARKDOWN[@]}"; do | ||
|  |     coloured 'yellow' "Processing: $file" | ||
|  |     OUTFILE="${file%.*}.html" | ||
|  | 
 | ||
|  |     if [[ $DRYRUN -eq 0 ]]; then | ||
|  |         # | ||
|  |         # Build nice HTML, disabling the smart quotes and stuff, but using | ||
|  |         # a local format definition and the HPR CSS. For now we just add | ||
|  |         # a title from the JSON. We could give Pandoc a block of YAML with | ||
|  |         # more attributes if we wished. | ||
|  |         # | ||
|  |         pandoc -f markdown-smart -t html5  --standalone \ | ||
|  |             --template=hpr.html5 -c https://hackerpublicradio.org/css/hpr.css \ | ||
|  |             --metadata title="$(jq -r '.episode.Title' "$JSONFILE")" --strip-comments \ | ||
|  |             -o "$OUTFILE" "${file}" | ||
|  |         coloured 'green' "Wrote: $OUTFILE" | ||
|  |     else | ||
|  |         coloured 'yellow' "Dry run mode: Would have written $OUTFILE" | ||
|  |     fi | ||
|  | done | ||
|  | 
 | ||
|  | exit | ||
|  | 
 | ||
|  | # vim: syntax=sh:ts=8:sw=4:ai:et:tw=78:fo=tcrqn21:fdm=marker | ||
|  | 
 |