Changeset 1229 in lmdz_wrf


Ignore:
Timestamp:
Oct 22, 2016, 3:59:15 PM (8 years ago)
Author:
lfita
Message:

Adding `ASCII_to': Function to provide the equivalence text to a given lenguage omitting certain characters

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tools/generic_tools.py

    r1228 r1229  
    3737####### Content
    3838# ASCII_LaTeX: Function to transform from an ASCII character to LaTeX codification
     39# ASCII_to: Function to provide the equivalence text to a given lenguage omitting certain characters
    3940# Capturing: Class to capture the standard output from a function
    4041# CFmonthU_daysU: Function to transform from a CF date series with units as 'months since [DATE]' to 'days since [DATE]'
     
    93959396    return newmat
    93969397
     9398def ASCII_to(text,to,nochg=None):
     9399    """ Function to provide the equivalence text to a given lenguage omitting certain characters
     9400      text= text to transform
     9401      to= language to use
     9402        'tex': LaTeX
     9403      nochg: a list with characters to be omitted
     9404    >>> ASCII_to('à$353\pol', 'tex')
     9405    \`a\$353\textbackslashpol
     9406    >>> ASCII_to('à$353\pol', 'tex', ['$'])
     9407    \`a$353\textbackslashpol
     9408    """
     9409    fname = 'ASCII_to'
     9410    langs = ['tex']
     9411
     9412
     9413    texchars = {'\\': '\\textbackslash', \
     9414      'á': "\\'a", 'é': "\\'e", 'í': "\\'i", 'ó': "\\'o", 'ú': "\\'u",               \
     9415      'à': "\\`a", 'Ú': "\\`e", 'ì': "\\`i", 'ò': "\\`o", 'ù': "\\`u",               \
     9416      'â': "\\^a", 'ê': "\\^e", 'î': "\\^i", 'ÃŽ': "\\^o", 'û': "\\^u",               \
     9417      'À': '\\"a', 'ë': '\\"e', 'ï': '\\"i', 'ö': '\\"o', 'ÃŒ': '\\"u',               \
     9418      'ç': '\c{c}', 'ñ': '\~{n}',                                                    \
     9419      'Á': "\\'A", 'É': "\\'E", 'Í': "\\'I", 'Ó': "\\'O", 'Ú': "\\'U",               \
     9420      'À': "\\`A", 'È': "\\`E", 'Ì': "\\`I", 'Ò': "\\`O", 'Ù': "\\`U",               \
     9421      'Â': "\\^A", 'Ê': "\\^E", 'Î': "\\^I", 'Ô': "\\^O", 'Û': "\\^U",               \
     9422      'Ä': '\\"A', 'Ë': '\\"E', 'Ï': '\\"I', 'Ö': '\\"O', 'Ü': '\\"U',               \
     9423      'Ç': '\\c{C}', 'Ñ': '\\~{N}',                                                  \
     9424      '¡': '!`', '¿': '¿`', '%': '\\%', '#': '\\#', '&': '\\&', '$': '\\$',          \
     9425      '_': '\\_', '·': '\\textperiodcentered', '<': '$<$', '>': '$>$',               \
     9426      '': '*', 'ª': '$^{a}$', 'º': '$^{o}$', '°': '$^{\\circ}$', '\n': '\\\\\n',   \
     9427      '\t': '\\medskip', '“': '``', '”': '\'\'', '^': '\^'}
     9428
     9429    if to == 'tex':
     9430        chars = texchars
     9431        # Specific first characters to change
     9432        firstchars = ['\\']
     9433    else:
     9434        print errormsg
     9435        print '  ' + fname + ": language '" + to + "' not ready !!"
     9436        print '    available languages:', langs
     9437        quit(-1)
     9438
     9439    if nochg is not None:
     9440        for noc in nochg:
     9441            if  not chars.has_key(noc):
     9442                print errormsg
     9443                print '  ' + fname + ": language '" + to + "' does not have '" +     \
     9444                  noc + "' to change !!"
     9445                quit(-1)
     9446            chars.pop(noc)
     9447            if searchInlist(firstchars,noc): firstchars.remove(noc)
     9448
     9449    newtext = text + ''
     9450    for char in firstchars:
     9451        newtext = newtext.replace(char,chars[char])
     9452        chars.pop(char)
     9453
     9454    for char in chars.keys():
     9455        newtext = newtext.replace(char,chars[char])
     9456
     9457    return newtext
     9458
    93979459def ASCII_LaTeX(ln):
    93989460    """ Function to transform from an ASCII character to LaTeX codification
     
    94179479    newln = newln.replace('â', "\\^a")
    94189480    newln = newln.replace('ê', "\\^e")
     9481
    94199482    newln = newln.replace('î', "\\^i")
    94209483    newln = newln.replace('ÃŽ', "\\^o")
     
    94889551      >>> latex_text('WRF_LMDZ$_{current}^{AR40}$')
    94899552      WRF\_LMDZ$_{current}^{AR40}$
     9553      >>> latex_text('Lluís Fita$_{1976/02/17}$')
     9554      Llu\'is Fita$_{1976/02/17}$
    94909555    """
    94919556    fname = 'latex_text'
     
    95879652        latextext = headtxt + '\\$' + tailtxt
    95889653
     9654    NOchange = ['_', '^', '\\', '&', '$']
     9655    latextext = ASCII_to(latextext, 'tex', NOchange)
     9656
    95899657    return latextext
    95909658
Note: See TracChangeset for help on using the changeset viewer.