]> Lady’s Gitweb - LesML/blob - sh/lesml.sh
Add some documentation to the transform
[LesML] / sh / lesml.sh
1 #!/usr/bin/env sh
2 # @(#)💄📝 Les·M·L sh/lesml.sh 2026-03-31T01:28:11Z
3 # SPDX-FileCopyrightText: 2023, 2024, 2026 Lady <https://www.ladys.computer/about/#lady>
4 # SPDX-License-Identifier: MPL-2.0
5
6 ## ⁌ The 💄📝 Les·M·L shell script
7 ##
8 ## ∎ Copyright © 2023–2024, 2026 Lady [@ Ladys Computer].
9 ##
10 ## ⋮ This Source Code Form is subject to the terms of the Mozilla
11 ## Public License, version 2.0.
12 ## If a copy of the M·P·L was not distributed with this file, You can
13 ## obtain one at {🔗<https://mozilla.org/MPL/2.0/>}.
14 ##
15 ## Usage :⁠—
16 ##
17 ## »|sh$sh ./sh/lesml.sh [--] [filename]
18 ##
19 ## This script provides a simple solution for converting a Les·M·L file
20 ## into X·H·T·M·L.
21
22 set -o 'errexit'
23
24 ## `LANG´ and `LC_ALL´ are set to `C´ because this script assumes
25 ## working with U·T·F‐8 strings as opaque series of bytes.
26
27 LANG=C
28 LC_ALL=C
29
30 ## § Configuration
31 ##
32 ## All of the commands used by this script are overridable with your
33 ## own implementations by setting the corresponding `cmd_COMMAND´
34 ## variable.
35
36 : "${cmd_DIRNAME:=dirname}"
37 : "${cmd_PRINTF:=printf}"
38 : "${cmd_REALPATH:=realpath}"
39 : "${cmd_SED:=sed}"
40 : "${cmd_TEST:=test}"
41 : "${cmd_TR:=tr}"
42 : "${cmd_XSLTPROC:=xsltproc}"
43
44 ## In order to run, this program needs to know the location of the
45 ## 💄📝 Les·M·L transform, which is calculated below.
46 ## This location can be manually configured by setting the `path_LESML´
47 ## or `path_TRANSFORM´ environment variables.
48
49 thisfile="$(
50 "${cmd_REALPATH}" -- "${0}"
51 )"
52 thisdir="$(
53 "${cmd_DIRNAME}" -- "${thisfile}"
54 )"
55
56 defaultlesml="$(
57 "${cmd_REALPATH}" -- "${thisdir}"'/..'
58 )"
59 : "${path_LESML:=${defaultlesml}}"
60
61 defaulttransform="${path_LESML}"'/xslt/lesml.xslt'
62 : "${path_TRANSFORM:=${defaulttransform}}"
63
64 ## The (unregistered) media type for Les·M·L files is `text/lesml´.
65 ## By overriding both the media type and the transform, one can re·use
66 ## this script for processing other kinds of files with X·S·L·T.
67
68 : "${name_MEDIATYPE:=text/lesml}"
69
70 ## § Parameter processing
71 ##
72 ## A leading `--´ is allowed (and ignored).
73 ## Otherwise, the first parameter is the source filename.
74
75 if "${cmd_TEST}" "${1}" = '--'
76 then :
77 shift
78 fi
79
80 ## If no input file is provided, it defaults to `-´ (standard input).
81
82 if "${cmd_TEST}" -n "${1}" && "${cmd_TEST}" "${1}" != '-'
83 then :
84 inputfile="$(
85 "${cmd_REALPATH}" -- "${1}"
86 )"
87 else :
88 inputfile='-'
89 fi
90
91 ## § Implementation
92 ##
93 ## ※ This implementation is derived from
94 ## {🔗⛩📰 书社<https://git.ladys.computer/Shushe/>} code written
95 ## 2023–2024.
96 ##
97 ## The `sanitizeprog´ variable contains a Sed program for sanitizing
98 ## an arbitrary string into characters which are valid in X·M·L.
99 ## Ideally this would be written using dollar‐single‐quote literals,
100 ## but Mac·O·S Dash is outdated and does not support them.
101
102 sanitizeprog="$(
103 "${cmd_PRINTF}" '%b' \
104 's/]]>/]]]]><!\\[CDATA\\[>/g\n' \
105 's/\0357\0277\0276/�/g\n' \
106 's/\0357\0277\0277/�/g\n' \
107 '$!s/\r$//g\n' \
108 's/\r/\\n/g\n' \
109 '$!s/\0302\0205$//g\n' \
110 's/\0302\0205/\\n/g\n' \
111 's/\0342\0200\0250/\\n/g\n' \
112 's/[\0001\0002\0003\0004\0005\0006\0007\0010]/�/g\n' \
113 's/[\0016\0017\0020\0021\0022\0023\0024\0025\0026\0027]/�/g\n' \
114 's/[\0031\0032\0033\0034\0035\0036\0037]/�/g'
115 )"
116
117 ## A compound statement is used to group together the wrapping X·M·L
118 ## and the sanitized input into one output which can be piped to
119 ## `xsltproc´.
120
121 {
122 "${cmd_PRINTF}" '%s\n%s%s%s' \
123 '<?xml version="1.0"?>' \
124 '<script xmlns="http://www.w3.org/1999/xhtml" type="' \
125 "${name_MEDIATYPE}" \
126 '"><![CDATA['
127 if "${cmd_TEST}" "${inputfile}" = '-'
128 then :
129 "${cmd_TR}" '\000\013\014' '\032\011\012'
130 else :
131 "${cmd_TR}" '\000\013\014' '\032\011\012' <"${inputfile}"
132 fi |
133 "${cmd_SED}" "${sanitizeprog}"
134 "${cmd_PRINTF}" '%s\n' ']]></script>'
135 } |
136 "${cmd_XSLTPROC}" --nonet --nowrite --novalid \
137 "${path_TRANSFORM}" \
138 -
This page took 0.077929 seconds and 5 git commands to generate.