From 4408c255d52f8c9418ba16c1f0054ecf76084e07 Mon Sep 17 00:00:00 2001 From: Dave Morriss Date: Sun, 1 Dec 2024 21:05:50 +0000 Subject: [PATCH] Added two new scripts Show_Submission/author_title.pl: Added the subtitle field taken from the JSON into the YAML Show_Submission/do_pandoc_assets: New script to process Markdown assets files. Not in use. --- Show_Submission/author_title.pl | 114 +++++++++++++++++++ Show_Submission/do_pandoc_assets | 189 +++++++++++++++++++++++++++++++ 2 files changed, 303 insertions(+) create mode 100755 Show_Submission/author_title.pl create mode 100755 Show_Submission/do_pandoc_assets diff --git a/Show_Submission/author_title.pl b/Show_Submission/author_title.pl new file mode 100755 index 0000000..2ed39d1 --- /dev/null +++ b/Show_Submission/author_title.pl @@ -0,0 +1,114 @@ +#!/usr/bin/env perl +#=============================================================================== +# +# FILE: author_title.pl +# +# USAGE: ./author_title.pl JSON_file YAML_file +# +# DESCRIPTION: Reads the JSON file generated as a new HPR show is uploaded +# via the submission form. Converts certain elements to a YAML +# structure which is written to a file for use by Pandoc when it +# generates a version of the notes used for local proof-reading. +# +# This function has been performed by 'jq' in the past, but with +# mixed success. Also 'yq' has been used experimentally with +# little success because of the wide variety of the input JSON +# data. This script has been written to try and achieve parsing, +# data extraction and YAML generation in a more controllable +# way. +# +# OPTIONS: --- +# REQUIREMENTS: --- +# BUGS: --- +# NOTES: 2024-10-18 Modified the YAML to include the field Pandoc is +# looking for: 'subtitle', This adds another header to the full +# notes, which allows another check to be carried out. Also +# using Pandoc template 'hpr_dev.html' which omits the '' +# tag. +# AUTHOR: Dave Morriss (djm), Dave.Morriss@gmail.com +# VERSION: 0.0.3 +# CREATED: 2023-10-13 22:37:36 +# REVISION: 2024-10-18 16:17:14 +# +#=============================================================================== + +use v5.16; +use strict; +use warnings; +use feature qw{ postderef say signatures state try }; +no warnings + qw{ experimental::postderef experimental::signatures experimental::try }; + +use JSON; +use YAML::Tiny; +use Data::Dumper; + +# +# Version number (Incremented by Vim) +# +our $VERSION = '0.0.3'; + +# +# Script and directory names +# +( my $PROG = $0 ) =~ s|.*/||mx; +( my $DIR = $0 ) =~ s|/?[^/]*$||mx; +$DIR = '.' unless $DIR; + +#------------------------------------------------------------------------------- +# Declarations +#------------------------------------------------------------------------------- +# +# Constants and other declarations +# +my $basedir = "$ENV{HOME}/HPR/Show_Submission"; + +# +# Enable Unicode mode +# +binmode STDOUT, ":encoding(UTF-8)"; +binmode STDERR, ":encoding(UTF-8)"; + +# +# Check arguments +# +die "Usage: $PROG JSON_file YAML_file\n" if ( scalar(@ARGV) != 2 ); +my $json_file = shift; +my $yaml_file = shift; + +die "Unable to find $json_file\n" unless ( -e $json_file ); + +# +# Read and parse the JSON +# +my $json = JSON->new->utf8; + +open( my $jfile, "<:encoding(UTF-8)", $json_file ) + or die "Unable to open $json_file\n"; +my $json_text = <$jfile>; +close($jfile); + +my $content = decode_json($json_text); + +# +# Construct the YAML (is 'subtitle' better?) +# +my $yaml = YAML::Tiny->new( + { author => $content->{host}->{Host_Name}, + title => $content->{episode}->{Title}, + subtitle => $content->{episode}->{Summary}, + summary => $content->{episode}->{Summary}, + } +); +#say Dumper(\$yaml); + +# +# Output the YAML +# +unless ( $yaml->write($yaml_file) ) { + warn "Problem writing the YAML to $yaml_file\n"; +} + +exit; + +# vim: syntax=perl:ts=8:sw=4:et:ai:tw=78:fo=tcrqn21:fdm=marker diff --git a/Show_Submission/do_pandoc_assets b/Show_Submission/do_pandoc_assets new file mode 100755 index 0000000..7886218 --- /dev/null +++ b/Show_Submission/do_pandoc_assets @@ -0,0 +1,189 @@ +#!/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 +