/******************************************************************************/ /* */ /* RexxPubOptions.cls -- Shared option handling for RexxPub pipelines */ /* ================================================================= */ /* */ /* This file is part of the Rexx Parser package */ /* [See https://rexx.epbcn.com/rexx-parser/] */ /* */ /* Copyright (c) 2024-2026 Josep Maria Blasco */ /* */ /* License: Apache License 2.0 (https://www.apache.org/licenses/LICENSE-2.0) */ /* */ /* Version history: */ /* */ /* Date Version Details */ /* -------- ------- --------------------------------------------------------- */ /* 20260313 0.5 Created: refactor common code from CGI, md2pdf, md2html */ /* 20261018 css: (a sheet, several, or a YAML list) and margin: */ /* (Rony: a document's own sheet and margins, for md2pdf) */ /* 20261027 A value that should be one, and is a list or a block */ /* (style: {...}), is said and left out: md2slides died on */ /* it, and md2html called it 'a StringTable' */ /* 20261102 cover: and chapter-level: (md2epub's) are read */ /* 20261119 No ::Requires of ANSI.ErrorText.cls, which is gone (it */ /* was not used) */ /* 20261121 Moved from bin/parser/ to bin/rexxpub/: it is RexxPub's, */ /* not part of the parser (RFE #878) */ /* */ /******************************************************************************/ /******************************************************************************/ /* */ /* ParseRexxPubYAML(yaml) -- Extract RexxPub options from parsed YAML */ /* */ /* 'yaml' is the StringTable returned by YAMLFrontMatter(), or .nil. */ /* */ /* Returns a StringTable with normalized option values: */ /* */ /* Top-level (from rexxpub: block): */ /* style - Rexx highlighting style (or .Nil if not set) */ /* size - base font size (or .Nil if not set) */ /* section-numbers - numbering depth (or .Nil if not set) */ /* number-figures - 0 or 1 (or .Nil if not set) */ /* docclass - document class name (or .Nil if not set) */ /* outline - PDF bookmark depth (or .Nil if not set) */ /* executor - "true" if Executor set here (or .Nil) */ /* experimental - "true" if Experimental set here (or .Nil) */ /* unicode - "true" if TUTOR/Unicode set here (or .Nil); */ /* "tutor" is folded into this key */ /* */ /* Listing sub-options (from rexxpub: listings:): */ /* listing-caption-position - "above" or "below" (default: "above") */ /* listing-caption-style - "normal" or "italic" (default: "normal") */ /* listing-label-style - "bold", "italic", etc. (default: "bold") */ /* listing-label - custom label text (default: "") */ /* listing-frame - "none", "tb", "single", "leftbar" */ /* (default: "none") */ /* */ /* Figure sub-options (from rexxpub: figures:): */ /* figure-caption-position - "above" or "below" (default: "below") */ /* figure-caption-style - "normal" or "italic" (default: "normal") */ /* figure-label-style - "bold", "italic", etc. (default: "bold") */ /* figure-label - custom label text (default: "") */ /* */ /* Pandoc metadata (from top-level YAML): */ /* language - HTML lang attribute (Pandoc variable */ /* 'lang'; or .Nil if not set) */ /* highlight-style - Pandoc highlighting theme */ /* (or .Nil if not set) */ /* */ /******************************************************************************/ ::Routine ParseRexxPubYAML Public Use Strict Arg yaml opts = .StringTable~new -- Top-level defaults (.Nil = "not set by YAML") opts["style"] = .Nil opts["size"] = .Nil opts["section-numbers"] = .Nil opts["number-figures"] = .Nil opts["docclass"] = .Nil opts["language"] = .Nil opts["outline"] = .Nil opts["chapter"] = .Nil opts["css"] = .Nil -- .Nil, or an Array of names opts["margin"] = .Nil -- Language-dialect defaults (.Nil = "not set by YAML"). These are -- additive/monotonic: a dialect is enabled for a block if it is set -- here, on the CLI, or on the fence. There is no way to disable a -- dialect from the YAML (no "false"): the only meaningful value is -- "true". opts["executor"] = .Nil opts["experimental"] = .Nil opts["unicode"] = .Nil opts["tutor"] = .Nil -- Listing sub-option defaults opts["listing-caption-position"] = "above" opts["listing-caption-style"] = "normal" opts["listing-label-style"] = "bold" opts["listing-label"] = "" opts["listing-frame"] = "none" -- Figure sub-option defaults opts["figure-caption-position"] = "below" opts["figure-caption-style"] = "normal" opts["figure-label-style"] = "bold" opts["figure-label"] = "" -- Pandoc metadata (.Nil = "not set by YAML") opts["highlight-style"] = .Nil If yaml == .Nil Then Return opts -- Process first-level entries -- Read highlight-style from top-level YAML (standard Pandoc metadata) If yaml~hasIndex("highlight-style") Then opts["highlight-style"] = Lower(yaml["highlight-style"]) -- Read lang from top-level YAML (standard Pandoc metadata). -- Stored internally as "language" for historical reasons. If yaml~hasIndex("lang") Then opts["language"] = yaml["lang"] -- Process second- and third-level entries If yaml~hasIndex("rexxpub") Then Call ReadRexxPubBlock Return opts ConditionalCopyFrom: Procedure Expose opts prefix Use Arg table, key, lowerCase = 0, list = "" newValue = table[key] If newValue == .Nil Then Return -- One value, not a list or a block: 'style: {.x}' is a block, and was -- taken for a style called 'a StringTable' (md2slides died on it) If \newValue~isA(.String) Then Do name = key If prefix \== "" Then name = prefix"s:" key If newValue~isA(.Array) Then what = "a list" Else what = "a block" .Error~Say( "Warning: YAML 'rexxpub:" name":' is" what", not a value; ignored." ) Return End If lowerCase Then newValue = Lower(newValue) If list \== "", WordPos(newValue, list) == 0 Then Return newKey = key If prefix \== "" Then newKey = prefix"-"key If table~hasIndex(key) Then opts[newKey] = newValue Return ReadRexxPubBlock: -- Read rexxpub: block rp = yaml["rexxpub"] If \rp~isA(.StringTable) Then Return lowerCase = 1 prefix = "" -- Process second-level entries Call ConditionalCopyFrom rp, "style" Call ConditionalCopyFrom rp, "size" Call ConditionalCopyFrom rp, "section-numbers" Call ConditionalCopyFrom rp, "number-figures" , lowerCase Call ConditionalCopyFrom rp, "docclass" , lowerCase Call ConditionalCopyFrom rp, "outline" Call ConditionalCopyFrom rp, "chapter" Call ConditionalCopyFrom rp, "margin" -- (20261102) md2epub's: they were documented, and md2epub looked for them, -- but they were never copied here Call ConditionalCopyFrom rp, "cover" Call ConditionalCopyFrom rp, "chapter-level" -- css: one sheet, several (blanks or commas), or a YAML list. The names -- are the document's to resolve (relative to it): here they are only -- gathered. A name with blanks in it is given as a YAML list. If rp~hasIndex("css") Then Do value = rp["css"] names = .Array~new If value~isA(.Array) Then Do Do item Over value If item~isA(.String), Strip(item) \== "" Then names~append(Strip(item)) End End Else If value~isA(.String) Then Do rest = value~changeStr(",", " ") Do While rest \== "" Parse Var rest name rest names~append(name) End End If names~items > 0 Then opts["css"] = names End -- Language dialects (additive; only "true" has an effect) Call ConditionalCopyFrom rp, "executor" , lowerCase Call ConditionalCopyFrom rp, "experimental", lowerCase Call ConditionalCopyFrom rp, "unicode" , lowerCase Call ConditionalCopyFrom rp, "tutor" , lowerCase -- Sanitize values opt = opts["number-figures"] If opt \== .Nil Then Do Select Case opt When 0, 1 Then Nop When "true" Then opts["number-figures"] = 1 When "false" Then opts["number-figures"] = 0 Otherwise opts~remove("number-figures") End End outline = opts["outline"] If DataType(outline,"W"), outline >= 0, outline <= 6 Then Nop Else opts~remove("outline") level = opts["chapter-level"] If level \== .Nil Then Do If DataType(level,"W"), level >= 1, level <= 6 Then Nop Else opts~remove("chapter-level") End -- Dialects: keep only "true", drop anything else. "tutor" is an -- alias of "unicode" (as with the --tutor/--unicode CLI flags), so -- fold it into "unicode" and leave a single key for consumers. Do dialect Over .Array~of("executor", "experimental", "unicode", "tutor") If opts[dialect] \== "true" Then opts~remove(dialect) End If opts["tutor"] == "true" Then opts["unicode"] = "true" opts~remove("tutor") -- Process third-level entries -- listings: sub-table If rp~hasIndex("listings") Then Call ProcessListings -- figures: sub-table If rp~hasIndex("figures") Then Call ProcessFigures Return ProcessListings: lst = rp["listings"] If \lst~isA(.StringTable) Then Return prefix = "listing" Call ConditionalCopyFrom lst, "caption-position", lowerCase, "above below" Call ConditionalCopyFrom lst, "caption-style", lowerCase, "normal italic" Call ConditionalCopyFrom lst, "label-style", lowerCase, - "normal bold italic bold-italic" Call ConditionalCopyFrom lst, "frame", lowerCase, - "none tb single leftbar" Call ConditionalCopyFrom lst, "label" Return ProcessFigures: fig = rp["figures"] If \fig~isA(.StringTable) Then Return prefix = "figure" Call ConditionalCopyFrom fig, "caption-position", lowerCase, "above below" Call ConditionalCopyFrom fig, "caption-style", lowerCase, "normal italic" Call ConditionalCopyFrom fig, "label-style", lowerCase, - "normal bold italic bold-italic" Call ConditionalCopyFrom fig, "label" Return /******************************************************************************/ /* */ /* BuildCaptionOverrides(opts) -- Generate CSS override string */ /* */ /* 'opts' is the StringTable returned by ParseRexxPubYAML(). */ /* */ /* Returns a StringTable with three keys: */ /* "overrideCSS" - CSS rules to inject as a