#! /usr/bin/gawk -f
# Last edited on 2016-05-09 23:27:30 by stolfilocal
# To be loaded into {wds-to-plaintext.gawk}

function tex_recode(pword,ptype,math,type,word,act,  ptw,befo,wtex,neol)
  { 
    # Converts {word} of the given {type} to a LaTeX construct.
    # The {type} is "p" for punct, "a" for alpha, "s" for symbol,
    # etc. 
    
    # Also gets the previous word {pword} and its type {ptype}
    # (or "" for the first word).

    # The boolean {math} tells whether the text
    # is currently in math mode.

    # Returns the result in the prealocated array {act[1..]}:
    #
    #  {act[1]} the spaces to be inserted before the word,
    #  if there is no line break. 
    #
    #  {act[2]} the text to be appended to the line.
    
    # Previous type and word: */
    ptw = (ptype pword);
    if ((ptw == "p=") || (ptw == "p÷")) { ptw = ""; }

    # Defaults: 
    befo = ""; 
    wtex = word;
    neol = 0;
    # Decide the spaces {befo} to add before before (or flush at end of parag and set {word} to nil):
    if (type == "p")
      { # Punctuation:
        if (word == "(")
          { befo = " "; }
        else if (word == "«")
          { befo = ((ptw == "") ? "" : " ");
            wtex = "``";
          }
        else if (word == "»")
          { wtex = "''"; }
        else if (word == "<")
          { befo = " ";
            wtex = "`";
          }
        else if (word == ">")
          { wtex = "'"; }
      }
    else
      { # Alpha or symbol:
        wtex = word;
        if ((ptw == "") || (ptw == "p(") || (ptw == "p«") || (ptw ~ /[-°]$/) || (ptw == "p<"))
          { befo = ""; }
        else
          { befo = " "; }
      }

    # Recode some symbols: 
    gsub(/[~]/, "-", wtex);
    gsub(/[°]/, ".~", wtex);
    
    # Return results:
    act[1] = befo;
    act[2] = wtex;
  }