LGR Transcription to Greek LICR transformation

Licence

This work may be distributed and/or modified under the conditions of the LaTeX Project Public License, either version 1.3 of this license or any later version.

The LGR font encoding is the de-facto standard for Greek typesetting with LaTeX. This file provides a translation from the Latin transcription defined by LGR into the LaTeX Internal Character Representation (LICR) macros.

usage = [[
Usage: lua lgr2licr.lua [OPTIONS] [STRING]
  Convert STRING from Latin transcription to LICR macros for Greek symbols.
  Without argument, the script reads from standard input, e.g., a
  redirected file or interactive input ended with Ctrl-D.
Options: -h, --help      show this help
]]

if arg[1] == "-h" or arg[1] == "--help" then
    print(usage)
    return
end

Get input string:

if ... then
    s = string.gsub(..., "\t", " ")
else
    s = io.read("*all")
end

The mapping from the LGR Latin transcription to LICR macros:

LGR_map = {
  A = "\\textAlpha{}",
  B = "\\textBeta{}",
  G = "\\textGamma{}",
  D = "\\textDelta{}",
  E = "\\textEpsilon{}",
  Z = "\\textZeta{}",
  H = "\\textEta{}",
  J = "\\textTheta{}",
  I = "\\textIota{}",
  K = "\\textKappa{}",
  L = "\\textLambda{}",
  M = "\\textMu{}",
  N = "\\textNu{}",
  K = "\\textXi{}",
  O = "\\textOmicron{}",
  P = "\\textPi{}",
  R = "\\textRho{}",
  S = "\\textSigma{}",
  T = "\\textTau{}",
  U = "\\textUpsilon{}",
  F = "\\textPhi{}",
  Q = "\\textChi{}",
  Y = "\\textPsi{}",
  W = "\\textOmega{}",

  a = "\\textalpha{}",
  b = "\\textbeta{}",
  g = "\\textgamma{}",
  d = "\\textdelta{}",
  e = "\\textepsilon{}",
  z = "\\textzeta{}",
  h = "\\texteta{}",
  j = "\\texttheta{}",
  i = "\\textiota{}",
  k = "\\textkappa{}",
  l = "\\textlambda{}",
  m = "\\textmu{}",
  n = "\\textnu{}",
  x = "\\textxi{}",
  o = "\\textomicron{}",
  p = "\\textpi{}",
  r = "\\textrho{}",
  s = "\\textautosigma{}",
  c = "\\textfinalsigma{}",
  t = "\\texttau{}",
  u = "\\textupsilon{}",
  f = "\\textphi{}",
  q = "\\textchi{}",
  y = "\\textpsi{}",
  w = "\\textomega{}",
  v = "\\noboundary{}",

  ["'"] = "\\'",
  ["`"] = "\\`",
  ["~"] = "\\~",
  ["<"] = "\\<",
  [">"] = "\\>",
  ["|"] = "\\|",
  ['"'] = '\\"',
  [";"] = "\\textanoteleia{}",
  ["?"] = "\\texterotimatiko{}",
}

Replace the “autosigma” (s) with “sigma” (sv) if followed by another character and with “finalsigma” (c) if followed by space or punctuation:

s = string.gsub(s, "s([a-z'`~<>])", "sv%1")
s = string.gsub(s, "s([ ,.:;?!])", "c%1")
s = string.gsub(s, 's"(%s)', 'c"%1')  -- " before whitespace is quote/apostrophe

Use the mapping to replace every ASCII-character with non-standard meaning to the corresponding LICR macro:

s = string.gsub(s, "([a-zA-Z'`~<>|\";?])", LGR_map)

Some cleanup operations:

s = string.gsub(s, "\\'\\'", "\\textquoteright{}")               -- ''
s = string.gsub(s, "\\`\\`", "\\textquoteleft{}")                -- ``
s = string.gsub(s, "\\textautosigma{}\\noboundary{}", "\\textsigma{}") -- sv
s = string.gsub(s, "{}\\", "\\")

The quotation mark (“) denotes a quote/apostrophe if followed by a space:

s = string.gsub(s, '\"(%s)', "\\textquoteright{}%1")

Write the result to stdout:

io.write(s)