[luatex] lua script on the run

Philipp Stephani st_philipp at yahoo.de
Thu Jan 6 23:05:22 CET 2011


Am 06.01.2011 um 15:00 schrieb Khaled Hosny:

>> And before you arrive at a node list, TeX will already have complained about ^ outside math mode. Probably a token_filter callback would work better.
> 
> I was not thinking about this particular case, one can use tex-friendly
> transliteration scheme but still need to escape non-textual material
> which makes node list processing appealing. I would have suggested
> token_filter but tex tokens are beyond my understanding.

Sadly, the documentation for the token library is quite incomplete and sometimes incorrect, but finally I got it to work:

\input luatexbase.sty
\input luaotfload.sty
\directlua{%
  local translate = {
    c = "ĉ",
    g = "ĝ",
    h = "ĥ",
    j = "ĵ",
    s = "ŝ",
    u = "û"
  }
  local function get_char_token(char)
    return token.create(unicode.utf8.byte(char))
  end
  local function get_token_char(tok)
    return tok[1] == 11 and unicode.utf8.char(tok[2])
  end
  local circumflex_token = get_char_token("^")
  local function is_circumflex(tok)
    return tok[1] == circumflex_token[1]
       and tok[2] == circumflex_token[2]
       and tok[3] == circumflex_token[3]
  end
  local function in_math_mode()
    local n = tex.nest[tex.nest.ptr]
    return n.mode == 235 or n.mode == -235
  end
  local function texperanto()
    local t = token.get_next()
    if not in_math_mode() and is_circumflex(t) then
      local u = token.get_next()
      local c = get_token_char(u)
      if c then
        local d = translate[c]
        if d then
          return get_char_token(d)
        else
          return {t, u}
        end
      else
        return {t, u}
      end
    else
      return t
    end
  end
  luatexbase.add_to_callback("token_filter", texperanto, "texperanto")
}
\font\rm={name:DejaVu Sans:}\relax
\rm
^c ^g ^h ^j ^s ^u $b^c$
\bye




More information about the luatex mailing list