]>
Lady’s Gitweb - Codemark/blob - sh/codemark.sh
2 # SPDX-FileCopyrightText: 2026 Lady <https://www.ladys.computer/about/#lady>
3 # SPDX-License-Identifier: MPL-2.0
5 ## ⋯ 🧮🖍 Codemark ∷ sh ∷ codemark.sh
7 ## ⁌ The 🧮🖍 Codemark shell script
9 ## ] Copyright © 2026 Lady [@ Ladys Computer].
11 ## ] This Source Code Form is subject to the terms of the Mozilla
12 ## ] Public License, version 2.0.
13 ## ] If a copy of the M·P·L was not distributed with this file, You can
14 ## ] obtain one at {🔗<https://mozilla.org/MPL/2.0/>}.
18 ## »|sh$ sh ./sh/codemark.sh [--] [filename] [syntax]
22 ## `set -o 'pipefail'´ is disabled because the (now quite old) version
23 ## of Dash bundled with Mac·O·S does not support it.
24 ## If it is ever upgraded to Posix 2024 compliance, then the option can
25 ## (and should) be enabled.
26 ## See the `sh´ directory at
27 ## {🔗<https://github.com/apple-oss-distributions/shell_cmds/>} for
28 ## the current source code for Mac·O·S Dash.
32 ## `LANG´ and `LC_ALL´ are set to `C´ because these scripts assume
33 ## working with U·T·F‐8 strings as opaque series of bytes.
38 ## § Variable defaults
40 ## All of the commands used by this script are overridable with your
41 ## own implementations by setting the corresponding `cmd_COMMAND´
43 ## The hope is that this provides a somewhat easier overriding
44 ## mechanism than those built into the shell (e·g defining a function
45 ## of the same name or overriding the `PATH´).
48 : "${cmd_BASENAME:=basename}"
50 : "${cmd_CKSUM:=cksum}"
53 : "${cmd_DIRNAME:=dirname}"
54 : "${cmd_MKDIR:=mkdir}"
56 : "${cmd_PRINTF:=printf}"
57 : "${cmd_REALPATH:=realpath}"
63 : "${cmd_XARGS:=xargs}"
65 ## Similarly, there are a few special paths that this program needs to
67 ## Their default values are calculated below.
70 "${cmd_REALPATH}" -- "${0}"
73 "${cmd_DIRNAME}" -- "${thisfile}"
75 defaultbuilddir
='@build.tmp/🧮🖍'
77 "${cmd_REALPATH}" -- "${thisdir}"'/..'
79 defaultsyntaxdir
="${path_CODEMARK}"'/sed/SYNTAXES'
81 : "${path_BUILDDIR:=${defaultbuilddir}}"
82 : "${path_CODEMARK:=${defaultcodemark}}"
83 : "${path_SYNTAXDIR:=${defaultsyntaxdir}}"
85 ## § Parameter processing
87 ## A leading `--´ is allowed (and ignored).
88 ## Otherwise, the first parameter is the source filename, and the
89 ## second the syntax id.
91 if "${cmd_TEST}" "${1}" = '--'
96 ## If no input file is provided, it defaults to `-´ (standard input).
98 if "${cmd_TEST}" -n "${1}" && "${cmd_TEST}" "${1}" != '-'
101 "${cmd_REALPATH}" -- "${1}"
107 ## If no syntax is provided, it is taken from the extension of the
108 ## input file (the characters following the final `.´).
109 ## If the input file does not have an extension, the syntax defaults to
113 if "${cmd_TEST}" -z "${mainsyntax}"
116 "${cmd_PRINTF}" '%s\n' "${inputfile}" |
117 "${cmd_SED}" '/[.][^/][^/]*$/!s/$/.lesml/;s/.*[.]\([^/]*\)$/\1/'
121 ## The `sedprog´ variable gives the filename of the Sed program which
122 ## describes the current syntax.
124 if "${cmd_TEST}" "${mainsyntax}" = 'lesml'
128 sedprog="${path_SYNTAXDIR}"'/'"${mainsyntax}"'.sed'
131 ## It is an error if the main syntax is not `lesml´ and has no syntax
134 if "${cmd_TEST}" -n "${sedprog}" && "${cmd_TEST}" ! -f "${sedprog}"
136 "${cmd_PRINTF}" '%s\n' 'Unrecognized syntax: '"${mainsyntax}"'.' >&2
140 ## § Processing the file
142 ## The input file is processed and saved to a temporary file whose
143 ## filename is generated from the current time and a hash of the
145 ## The hope is that this is enough to prevent collisions (assuming two
146 ## versions of this script do not attempt to process the same file at
150 "${cmd_PRINTF}" '%s\n' "${inputfile}" | "${cmd_CKSUM}"
153 "${cmd_DATE}" -u '+%Y%m%d%H%M%S'
155 tempfile="${path_BUILDDIR}"'/'"${temphash}"'@'"${temptime}"'.lesml'
157 ## Assuming the syntax is not `lesml´, the input file is processed to
158 ## produce Les·M·L output.
159 ## This is then saved to the tempfile.
160 ## If the syntax ⹐is⹑ `lesml´, the input file is simply copied.
162 if "${cmd_TEST}" "${mainsyntax}" = 'lesml'
164 if "${cmd_TEST}" "${inputfile}" = '-'
166 "${cmd_CAT}" >|"${tempfile}"
168 "${cmd_CP}" -- "${inputfile}" "${tempfile}"
171 if "${cmd_TEST}" "${inputfile}" = '-'
173 "${cmd_SED}" -f "${sedprog}" |
174 "${cmd_SED}" -f "${path_CODEMARK}"'/sed/syntax-comment.sed' |
175 "${cmd_SED}" 's//'"${mainsyntax}"'/g' >|"${tempfile}"
177 "${cmd_SED}" -f "${sedprog}" <"${inputfile}" |
178 "${cmd_SED}" -f "${path_CODEMARK}"'/sed/syntax-comment.sed' |
179 "${cmd_SED}" 's//'"${mainsyntax}"'/g' >|"${tempfile}"
183 ## Next, tagged code blocks in the Les·M·L need to be processed.
184 ## First, a list of syntaxes must be collected.
187 "${cmd_SED}" '/^»|[^ $]*$ /!d;s/»|\([^ $]*\).*/\1/' <"${tempfile}" |
189 "${cmd_TR}" -s '\n' ' '
192 ## Each syntax can then be processed.
193 ## Processing blanks out all lines not in the syntax, removes the
194 ## syntax prefix, processes the resulting file, and then restores the
195 ## blanked out lines.
196 ## Unlike with the main syntax, missing syntaxes at this stage are
201 syntaxprog="${path_SYNTAXDIR}/${s}.
sed"
202 if "${cmd_TEST}" -f "${syntaxprog}"
204 "${cmd_MV}" -- "${tempfile}" "${tempfile}"'~'
206 -e '/^»|'${s}'$ /!s/.*/\7f/;s/^»|'${s}'$ //' \
210 "${cmd_SED}" -f "${path_CODEMARK}"'/sed/syntax-comment.sed' |
211 "${cmd_SED}" 's//'${s}'/g;/./s/^/»/' |
214 'FNR==NR{a[NR]=$0;next};{r=/^»\|'${s}'\$ /};r{print a[FNR]};!r' \
217 "${cmd_RM}" -- "${tempfile}"'~'
221 ## Finally, the resulting file is written to standard output and
224 if "${cmd_TEST}" -z "$
(
225 "${cmd_SED}" -n '/#!lesml/p;q' <"${tempfile}"
228 defaultheader='#!lesml@'"${lesml_LANG}"'$ profile='"${lesml_PROFILE}"
229 : "${lesml_LANG:=en}"
230 : "${lesml_PROFILE:=urn:fdc:ladys.computer:20260226:codemark:pf:out}"
231 : "${lesml_HEADER:=${defaultheader}}"
232 "${cmd_PRINTF}" '%s\n' "${lesml_HEADER}"
234 "${cmd_CAT}" -- "${tempfile}"
235 "${cmd_RM}" -- "${tempfile}"
This page took 0.087624 seconds and 5 git commands to generate.