| 1 |
#!/usr/bin/env snobol4 |
| 2 |
# texaccents.sno |
| 3 |
# version 1.0.1 |
| 4 |
# 2022.08.23 -- 2022.09.17 |
| 5 |
# [email protected] |
| 6 |
# Transforms LaTeX accents to their UTF8 equivalents |
| 7 |
# accepts both LaTeX and Bibtex codes: |
| 8 |
# examples: \'a \'{a} {\"a} and even {\'{a}} |
| 9 |
# tables of accents imported from: |
| 10 |
# https://github.com/hayk314/LaTex-handler -- Author: Hayk Aleksanyan |
| 11 |
# with my integrations, including ligatures as \ae{} |
| 12 |
# requires snobol4: see http://www.regressive.org/snobol4/csnobol4/curr/ |
| 13 |
|
| 14 |
################################################################# |
| 15 |
# MIT License - Copyright (c) 2022 Guido Milanese |
| 16 |
# See file LICENSE in this package |
| 17 |
################################################################# |
| 18 |
|
| 19 |
****************************************************** |
| 20 |
* FUNCTIONS |
| 21 |
****************************************************** |
| 22 |
|
| 23 |
*-- Function INITIALISE |
| 24 |
*-- opens files, imports functions |
| 25 |
define("initialise()") |
| 26 |
:(initialise_end) |
| 27 |
initialise |
| 28 |
initialise_bg |
| 29 |
* INCLUDE FILES |
| 30 |
-include "delete.inc" |
| 31 |
-include "host.inc" |
| 32 |
-include "newline.inc" |
| 33 |
-include "grepl.inc" ;* essential! calls repl internally |
| 34 |
MAX = 8000000 ;* max size of input/output files = 8 megs |
| 35 |
NL = newline() |
| 36 |
HELP_MSG = "texaccents 1.0.1" NL |
| 37 |
+ "Converts legacy (La)TeX accents and ligatures to Unicode" NL |
| 38 |
+ "Usage: texaccents.sno INFILE OUTFILE" NL |
| 39 |
+ "--help print this help, then exit" NL |
| 40 |
+ "--version print version number, then exit" NL |
| 41 |
+ "Report bugs to <[email protected]>" NL |
| 42 |
+ "CTAN page of the package: <https://www.ctan.org/pkg/texaccents>" |
| 43 |
|
| 44 |
VERS_MSG = "texaccents 1.0.1" NL NL |
| 45 |
+ "Copyright (c) 2022 Guido Milanese" NL NL |
| 46 |
+ "This is free software; see the source for copying conditions. There is NO" NL |
| 47 |
+ "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE." NL nl |
| 48 |
+ "Written by Guido Milanese <[email protected]>" |
| 49 |
|
| 50 |
|
| 51 |
####################################################### |
| 52 |
# opens input / output files |
| 53 |
####################################################### |
| 54 |
* Defines and checks Input & Output |
| 55 |
* help request -- exits. Both "--help" and "-help" are valid |
| 56 |
((host(2,2) ? "-help") (Terminal = HELP_MSG)) :s(end) |
| 57 |
* version request -- exits. Both "--version" and "-version" are valid |
| 58 |
((host(2,2) ? "-version") (Terminal = VERS_MSG)) :s(end) |
| 59 |
* Normal input-output with error messages. |
| 60 |
* Error messages are used by MAIN if necessary |
| 61 |
(~(infile = host(2,2)) ((Terminal = HELP_MSG) (ERROR_MSG = "Input error"))) :s(freturn) |
| 62 |
(~(outfile = host(2,3)) ((Terminal = HELP_MSG) (ERROR_MSG = "Output error"))) :s(freturn) |
| 63 |
INPUT('r_f',1,"B " MAX, Infile) :f(freturn) |
| 64 |
Terminal = "Reading text from " Infile |
| 65 |
OUTPUT('w_f',2,"B " MAX, Outfile) :f(freturn) |
| 66 |
initialise_rt initialise = r_f :(return) |
| 67 |
initialise_end |
| 68 |
|
| 69 |
##################################################################################### |
| 70 |
*-- Function NORMACC |
| 71 |
*-- Changes TeX accents without curly brackets to the standard form |
| 72 |
*-- \'a --> \'{a} |
| 73 |
*-- and replaces ligatures such as \ae --> æ | \oe --> œ |
| 74 |
*-- requires GREPL |
| 75 |
##################################################################################### |
| 76 |
define("normacc(Pass)") |
| 77 |
P_shortacc = notany('{') |
| 78 |
+ ((('\' any("'`" '"' "^=~.")) . Pre_shortacc) |
| 79 |
+ (any(&ucase &lcase) . Letter_shortacc)) . V_shortacc |
| 80 |
:(normacc_end) |
| 81 |
normacc |
| 82 |
normacc_bg Pass ? P_shortacc :f(normacc_lg) |
| 83 |
Pass ? V_shortacc = Pre_shortacc '{' Letter_shortacc '}' :(normacc_bg) |
| 84 |
normacc_lg Pass2 = grepl(Pass, |
| 85 |
+ "\ae{} \oe{} \AE{} \OE{} \dh{} \DH{} \th{} \TH{} \o{} \O{} \l{} \L{} ", |
| 86 |
+ "æ œ Æ Œ ð Ð þ Þ ø Ø ł Ł ") |
| 87 |
Terminal = "Ligatures tranformed" |
| 88 |
normacc_rt normacc = Pass2 :(return) |
| 89 |
normacc_end |
| 90 |
* |
| 91 |
* The pattern to normalise accents -- as \'a -- is defined as follows: |
| 92 |
* 1. there must be no '{' before backslash: otherwise the pattern would trap also |
| 93 |
* BibTex codes such as {\'a} |
| 94 |
* 2. find a backslash followed by any char of the list. Saved as "Pre_sortacc" |
| 95 |
* 3. find the accented letter. Saved as Letter_shortacc |
| 96 |
* 4. The whole structure is saved as V_shortacc |
| 97 |
* The programme will save the whole structure and substitute it in the text with: |
| 98 |
* Pre_shortacc (as \') + '{' + Letter_shortacc + '}' |
| 99 |
* in this way the short form -- \'a -- is transformed to the canonical \'{a} |
| 100 |
* |
| 101 |
* Ligatures and special chars are simply replaced with GREPL. |
| 102 |
|
| 103 |
##################################################################################### |
| 104 |
*-- Function CLEANACC |
| 105 |
*-- Substitutes plain letters to Unicode accents, using the sets provides by TRANSACC |
| 106 |
*-- \'{a} will output á |
| 107 |
##################################################################################### |
| 108 |
define("cleanacc(Text,Acc1,Acc2)") :(cleanacc_end) |
| 109 |
* Acc1 is the original LaTeX code e.g. \"{a} |
| 110 |
* Acc2, at the time of calling the function, is e.g. \"{ä} |
| 111 |
cleanacc |
| 112 |
cleanacc_bg |
| 113 |
* removes paretheses: \"{ä} --> \"ä |
| 114 |
Acc2 = delete(Acc2,"{}") |
| 115 |
* moves 2 chars and saves what remains, i.e. the letter |
| 116 |
* \"ä --> ä |
| 117 |
Acc2 ? len(2) (rem . Acc2) |
| 118 |
* in text replaces e.g. \"{a} with ä |
| 119 |
* the substitution is done ONCE for the whole text |
| 120 |
Text = repl(Text,Acc1,Acc2) |
| 121 |
Terminal = "Working on " Acc2 |
| 122 |
cleanacc_rt cleanacc = Text :(return) |
| 123 |
cleanacc_end |
| 124 |
|
| 125 |
####################################################### |
| 126 |
*-- Function TRANSACC |
| 127 |
*-- transforms LaTeX/Bibtex accents to Unicode |
| 128 |
####################################################### |
| 129 |
define("transacc(Pass)") |
| 130 |
* Pass is the input text |
| 131 |
* |
| 132 |
* Accents used by Latex, e.g. \'{a} = acute accent over 'a' |
| 133 |
Lat_acc = |
| 134 |
+ any('"' "'" "`" 'H' '^' 'v' 'u' 'c' '.' 'd' 'k' '~' '=' 'b' 'r' ) |
| 135 |
+ . V_lat_acc |
| 136 |
* |
| 137 |
* The pattern says: |
| 138 |
* '\' + Lat_acc OR ('{\' + Lat_acc + '{' OR nothing) |
| 139 |
* any letter, saved as C_noacc + '}' + '}' OR nothing |
| 140 |
* Lat_acc is already defined above and saved ad V_lat_acc |
| 141 |
* Examples \'{a} -- {\'a} -- {\'{a}} (\'a was already "normalised") |
| 142 |
P_lat_code = |
| 143 |
+ ( |
| 144 |
+ (('\' Lat_acc '{') | ('{\' Lat_acc ('{' | '') )) |
| 145 |
+ (any(&lcase &ucase) . C_noacc) '}' ('}' | '') ) . V_lat_code |
| 146 |
:(transacc_end) |
| 147 |
transacc |
| 148 |
transacc_bg |
| 149 |
latcode_bg V_lat_code = ;* cleans previous value |
| 150 |
V_lat_code_2 = ;* cleans previous value |
| 151 |
* Scans input text to locate the pattern |
| 152 |
* if no other LaTeX accents, fails and returns to main |
| 153 |
* if yes, goes to the section that transforms this accent. |
| 154 |
* e.g. if the accent is umlaut, will jump to TR" |
| 155 |
* After each transformation we have: |
| 156 |
* 1. the original code, e.g. \"{a} or \"a or {\"a} |
| 157 |
* 2. the transformed code, e.g. \"{ä} or \"ä or {\"ä} |
| 158 |
* Each section calls CLEANCLODE |
| 159 |
* CLEANCODE will remove all the {}\ and the LaTeX accent |
| 160 |
* and substitute in the input text e.g. \"{ä} with ä |
| 161 |
Pass ? P_lat_code :f(latcode_nd) |
| 162 |
:($('TR' V_lat_acc)) |
| 163 |
|
| 164 |
TR" ;* umlaut |
| 165 |
V_lat_code_2 = grepl(V_lat_code, |
| 166 |
+ "A a B b C c E e H h I i K k M m N n O o P p Q q S s T t U u V v W w X x Y y Z z ", |
| 167 |
+ "Ä ä B̈ b̈ C̈ c̈ Ë ë Ḧ ḧ Ï ï K̈ k̈ M̈ m̈ N̈ n̈ Ö ö P̈ p̈ Q̈ q̈ S̈ s̈ T̈ ẗ Ü ü V̈ v̈ Ẅ ẅ Ẍ ẍ Ÿ ÿ Z̈ z̈ ") |
| 168 |
Pass = cleanacc(Pass,V_lat_code,V_lat_code_2) :(latcode_bg) |
| 169 |
|
| 170 |
TR' ;* acute |
| 171 |
V_lat_code_2 = grepl(V_lat_code, |
| 172 |
+ "A a B b C c D d E e F f G g H h I i J j K k L l M m N n O o P p Q q R r S s T t U u V v W w X x Y y Z z ", |
| 173 |
+ "Á á B́ b́ Ć ć D́ d́ É é F́ f́ Ǵ ǵ H́ h́ Í í J́ ȷ́ Ḱ ḱ Ĺ ĺ Ḿ ḿ Ń ń Ó ó Ṕ ṕ Q́ q́ Ŕ ŕ Ś ś T́ t́ Ú ú V́ v́ Ẃ ẃ X́ x́ Ý ý Ź ź ") |
| 174 |
Pass = cleanacc(Pass,V_lat_code,V_lat_code_2) :(latcode_bg) |
| 175 |
|
| 176 |
TRH ;* double acute - \H{o} Hungarian |
| 177 |
V_lat_code_2 = grepl(V_lat_code, |
| 178 |
+ "A a E e I i M m O o U u ", |
| 179 |
+ "A̋ a̋ E̋ e̋ I̋ i̋ M̋ m̋ Ő ő Ű ű ") |
| 180 |
Pass = cleanacc(Pass,V_lat_code,V_lat_code_2) :(latcode_bg) |
| 181 |
|
| 182 |
TR` ;* grave |
| 183 |
V_lat_code_2 = grepl(V_lat_code, |
| 184 |
+ "A a Æ æ E e H h I i K k M m N n O o R r S s T t U u V v W w X x Y y Z z ", |
| 185 |
+ "À à Æ̀ æ̀ È è H̀ h̀ Ì ì K̀ k̀ M̀ m̀ Ǹ ǹ Ò ò R̀ r̀ S̀ s̀ T̀ t̀ Ù ù V̀ v̀ Ẁ ẁ X̀ x̀ Ỳ ỳ Z̀ z̀ ") |
| 186 |
Pass = cleanacc(Pass,V_lat_code,V_lat_code_2) :(latcode_bg) |
| 187 |
|
| 188 |
TR^ ;* circumflex \^{o} |
| 189 |
V_lat_code_2 = grepl(V_lat_code, |
| 190 |
+ "A a B b C c D d E e G g H h I i J j K k L l M m N n O o R r S s T t U u V v W w X x Y y Z z ", |
| 191 |
+ "Â â B̂ b̂ Ĉ ĉ D̂ d̂ Ê ê Ĝ ĝ Ĥ ĥ Î î Ĵ ĵ K̂ k̂ L̂ l̂ M̂ m̂ N̂ n̂ Ô ô R̂ r̂ Ŝ ŝ T̂ t̂ Û û V̂ v̂ Ŵ ŵ X̂ x̂ Ŷ ŷ Ẑ ẑ ") |
| 192 |
Pass = cleanacc(Pass,V_lat_code,V_lat_code_2) :(latcode_bg) |
| 193 |
|
| 194 |
TRv ;* caron hraceck \v{s} |
| 195 |
* For accents that are also normal letters |
| 196 |
* we need to restore the accent to its original value |
| 197 |
* This applies to: v u c d k b r |
| 198 |
V_lat_code_2 = grepl(V_lat_code, |
| 199 |
+ "A a B b C c D d E e F f G g H h I i J j K k L l M m N n O o P p Q q R r S s T t U u V v W w X x Y y Z z ", |
| 200 |
+ "Ǎ ǎ B̌ b̌ Č č Ď ď Ě ě F̌ f̌ Ǧ ǧ Ȟ ȟ Ǐ ǐ J̌ ǰ Ǩ ǩ Ľ ľ M̌ m̌ Ň ň Ǒ ǒ P̌ p̌ Q̌ q̌ Ř ř Š š Ť ť Ǔ ǔ V̌ v̌ W̌ w̌ X̌ x̌ Y̌ y̌ Ž ž ") |
| 201 |
V_lat_code_2 = repl(V_lat_code_2,"\v̌","\v") |
| 202 |
Pass = cleanacc(Pass,V_lat_code,V_lat_code_2) :(latcode_bg) |
| 203 |
|
| 204 |
TRu ;* breve \u{o} |
| 205 |
V_lat_code_2 = grepl(V_lat_code, |
| 206 |
+ "A a C c E e G g I i K k M m N n O o P p R r T t U u V v X x Y y ", |
| 207 |
+ "Ă ă C̆ c̆ Ĕ ĕ Ğ ğ Ĭ ĭ K̆ k̆ M̆ m̆ N̆ n̆ Ŏ ŏ P̆ p̆ R̆ r̆ T̆ t̆ Ŭ ŭ V̆ v̆ X̆ x̆ Y̆ y̆ ") |
| 208 |
V_lat_code_2 = repl(V_lat_code_2,"\ŭ","\u") |
| 209 |
Pass = cleanacc(Pass,V_lat_code,V_lat_code_2) :(latcode_bg) |
| 210 |
|
| 211 |
TRc ;* cedilla \c{c} |
| 212 |
V_lat_code_2 = grepl(V_lat_code, |
| 213 |
+ "A a B b C c D d E e G g H h I i K k L l M m N n O o Q q R r S s T t U u X x Z z ", |
| 214 |
+ "A̧ a̧ B̧ b̧ Ç ç Ḑ ḑ Ȩ ȩ Ģ ģ Ḩ ḩ I̧ i̧ Ķ ķ Ļ ļ M̧ m̧ Ņ ņ O̧ o̧ Q̧ q̧ Ŗ ŗ Ş ş Ţ ţ U̧ u̧ X̧ x̧ Z̧ z̧ ") |
| 215 |
V_lat_code_2 = repl(V_lat_code_2,"\ç","\c") |
| 216 |
Pass = cleanacc(Pass,V_lat_code,V_lat_code_2) :(latcode_bg) |
| 217 |
|
| 218 |
TR. ;* dot \.{o} |
| 219 |
V_lat_code_2 = grepl(V_lat_code, |
| 220 |
+ "A a B b C c D d E e F f G g H h I i K k L l M m N n O o P p Q q R r S s T t U u V v W w X x Y y Z z ", |
| 221 |
+ "Ȧ ȧ Ḃ ḃ Ċ ċ Ḋ ḋ Ė ė Ḟ ḟ Ġ ġ Ḣ ḣ İ i̇̀ K̇ k̇ L̇ l̇ Ṁ ṁ Ṅ ṅ Ȯ ȯ Ṗ ṗ Q̇ q̇ Ṙ ṙ Ṡ ṡ Ṫ ṫ U̇ u̇ V̇ v̇ Ẇ ẇ Ẋ ẋ Ẏ ẏ Ż ż ") |
| 222 |
Pass = cleanacc(Pass,V_lat_code,V_lat_code_2) :(latcode_bg) |
| 223 |
|
| 224 |
TRd ;* dot under the letter \d{u} |
| 225 |
V_lat_code_2 = grepl(V_lat_code, |
| 226 |
+ "A a B b C c D d E e F f G g H h I i J j K k L l M m N n O o P p Q q R r S s T t U u V v W w X x Y y Z z ", |
| 227 |
+ "Ạ ạ Ḅ ḅ C̣ c̣ Ḍ ḍ Ẹ ẹ F̣ f̣ G̣ g̣ Ḥ ḥ Ị ị J̣ j̣ Ḳ ḳ Ḷ ḷ Ṃ ṃ Ṇ ṇ Ọ ọ P̣ p̣ Q̣ q̣ Ṛ ṛ Ṣ ṣ Ṭ ṭ Ụ ụ Ṿ ṿ Ẉ ẉ X̣ x̣ Ỵ ỵ Ẓ ẓ ") |
| 228 |
V_lat_code_2 = repl(V_lat_code_2,"\ḍ","\d") |
| 229 |
Pass = cleanacc(Pass,V_lat_code,V_lat_code_2) :(latcode_bg) |
| 230 |
|
| 231 |
TRk ;* ogonek \k{a} |
| 232 |
V_lat_code_2 = grepl(V_lat_code, |
| 233 |
+ "A a E e I i O o U u Y y ", |
| 234 |
+ "Ą ą Ę ę Į į Ǫ ǫ Ų ų Y̨ y̨ ") |
| 235 |
Pass = cleanacc(Pass,V_lat_code,V_lat_code_2) :(latcode_bg) |
| 236 |
|
| 237 |
TR~ ;* tilde \~o |
| 238 |
V_lat_code_2 = grepl(V_lat_code, |
| 239 |
+ "A a E e I i N n O o U u V v Y y ", |
| 240 |
+ "Ã ã Ẽ ẽ Ĩ ĩ Ñ ñ Õ õ Ũ ũ Ṽ ṽ Ỹ ỹ ") |
| 241 |
Pass = cleanacc(Pass,V_lat_code,V_lat_code_2) :(latcode_bg) |
| 242 |
|
| 243 |
TR= ;* macron \=a \={a} |
| 244 |
V_lat_code_2 = grepl(V_lat_code, |
| 245 |
+ "A a B b C c D d E e G g I i J j K k M m N n O o P p Q q R r S s T t U u V v W w X x Y y Z z ", |
| 246 |
+ "Ā ā B̄ b̄ C̄ c̄ D̄ d̄ Ē ē Ḡ ḡ Ī ī J̄ j̄ K̄ k̄ M̄ m̄ N̄ n̄ Ō ō P̄ p̄ Q̄ q̄ R̄ r̄ S̄ s̄ T̄ t̄ Ū ū V̄ v̄ W̄ w̄ X̄ x̄ Ȳ ȳ Z̄ z̄ ") |
| 247 |
Pass = cleanacc(Pass,V_lat_code,V_lat_code_2) :(latcode_bg) |
| 248 |
|
| 249 |
TRb ;* bar under the letter \b{a} |
| 250 |
V_lat_code_2 = grepl(V_lat_code, |
| 251 |
+ "A a B b C c D d E e G g H h I i J j K k L l M m N n O o P p R r S s T t U u X x Y y Z z ", |
| 252 |
+ "A̱ a̱ Ḇ ḇ C̱ c̱ Ḏ ḏ E̱ e̱ G̱ g̱ H̱ ẖ I̱ i̱ J̱ j̱ Ḵ ḵ Ḻ ḻ M̱ m̱ Ṉ ṉ O̱ o̱ P̱ p̱ Ṟ ṟ S̱ s̱ Ṯ ṯ U̱ u̱ X̱ x̱ Y̱ y̱ Ẕ ẕ ") |
| 253 |
V_lat_code_2 = repl(V_lat_code_2,"\ḇ","\b") |
| 254 |
Pass = cleanacc(Pass,V_lat_code,V_lat_code_2) :(latcode_bg) |
| 255 |
|
| 256 |
TRr ;* ring over the letter \r{a} |
| 257 |
V_lat_code_2 = grepl(V_lat_code, |
| 258 |
+ "A a B b C c D d E e F f G g H h I i J j K k L l M m N n O o P p Q q r R S s U u V v W w X x Y y Z z ", |
| 259 |
+ "Å å B̊ b̊ C̊ c̊ D̊ d̊ E̊ e̊ F̊ f̊ G̊ g̊ H̊ h̊ I̊ i̊ J̊ j̊ K̊ k̊ L̊ l̊ M̊ m̊ N̊ n̊ O̊ o̊ P̊ p̊ Q̊ q̊ r̊ R̊ S̊ s̊ Ů ů V̊ v̊ W̊ ẘ X̊ x̊ Y̊ ẙ Z̊ z̊ ") |
| 260 |
V_lat_code_2 = repl(V_lat_code_2,"\r̊","\r") |
| 261 |
Pass = cleanacc(Pass,V_lat_code,V_lat_code_2) :(latcode_bg) |
| 262 |
|
| 263 |
latcode_nd |
| 264 |
transacc = Pass :(return) |
| 265 |
transacc_end |
| 266 |
|
| 267 |
####################################################### |
| 268 |
# MAIN |
| 269 |
####################################################### |
| 270 |
(~(texfile = initialise()) (terminal = "Could not start programme - " ERROR_MSG)) :s(end) |
| 271 |
Texfile = normacc(Texfile) |
| 272 |
(((w_f = transacc(texfile)) (Terminal = "Done. File " Outfile " written")), |
| 273 |
+ (Terminal = "Conversion failed")) |
| 274 |
end |