#!/usr/bin/env python """ SYNOPSIS Usage: prepare_tex [-h/-?/--h/--help] [-d] [--version=value/-v value...] \ [--translator=value/-t value...] [-lang=value...] [--output_file=value/-o \ value...] latex_src_file **-h/-?/--help/--h** Show this little help **-d** Show the whole doc string of this script **--version=value (-v)** *default version=2.0* Set the version of the translation **--output_file (-o)** *default outputfile=inputfile+_+lang* Set the output file destination **--translator=value (-t)** *default translator='unknown'* Set the name of the translator **-lang=value(-l)** *default lang=FR* Prepare for French(fr) or other, as you want For most parameters an interactive input will be given if the arg is not in the command line. Except output_file which is STDOUT by default. Result of this script : For each paragraph of the source file given in the command line: %%% TRANSLATION ORIG % original lines % of the paragraph %%% TRANSLATION FR # ready to write here %%% /TRANSLATION And we add a translation INFO paragraph in the header: %%% TRANSLATION INFO :\n") % Original : release 2.0 % Translation : CVS Id % Translator : Unknow % Language : FR FILE $Id: prepare_tex.py,v 1.5 2000/10/24 20:25:25 olberger Exp $ DESCRIPTION *Prepare_tex* Generate to output a new Latex file with comments and tokens for translation. AUTHOR Odile Bénassy, developped for the French Python Translation Workgroup. Contributions by Olivier Berger and Regis Leroy LICENCE GNU General Public License HISTORY $Log: prepare_tex.py,v $ Revision 1.5 2000/10/24 20:25:25 olberger Typo et correction mineure Revision 1.4 2000/10/24 14:36:14 olberger Ajout version améliorée encore par Régis Leroy - Revision 1.4 2000/10/24 15:30:00 rage o All args can be given in command line, if an arg is missing there is an interactive input. o To get the doc string the command line arg is now -d o add --help and --h to command line args. o add a TODO section - Revision 1.3 2000/10/22 10:22:25 rage o Doc strings corrections, gendoc compatibility, cosmetic changes. o command line parser o add possibility of changing the 'FR' token - Revision 1.2 2000/10/23 obenassy o Interactive input of header parameters o Header generation - Revision 1.1 2000/10/18 15:47:07 oberger o some improvements on limit cases o CVS integration - Version 1.0 2000/10/18 15:47:07 Original author : Odile Bénassy, developped for the French Python Translation Workgroup. (http://sourceforge.net/project/frpython). TODO - a --reverse function to get the original copy ? - verifying existence of the output file before writing on it. - add a space after 'release' in the output? """ ######### # IMPORTS import sys, string, re, os, getopt ######### # GLOBALS BLANK = re.compile('^[%s]*$' % string.whitespace) LANG_TOKEN = "" TRANSLATOR = "" VERSION = "" OUTPUTFILE = "" __author__ = "Odyle Benassy" # CVS crud __version__ = "$Revision: 1.5 $" # $Source: /cvsroot/frpython/translation/fr/outils/prepare_tex.py,v $ __usage__ = """ Usage: prepare_tex [-h/-?/--h/--help] [-d] [--version=value/-v value...] \ [--translator=value/-t value...] [-lang=value...] [--output_file=value/-o \ value...] latex_src_file **-h/-?/--help/--h** Show this little help **-d** Show the whole doc string of this script **--version=value (-v)** *default version=2.0* Set the version of the translation **--output_file (-o)** *default outputfile=inputfile+_+lang* Set the output file destination **--translator=value (-t)** *default translator='unknown'* Set the name of the translator **-lang=value(-l)** *default lang=FR* Prepare for French(fr) or other, as you want For most parameters an interactive input will be given if the arg is not in the command line. Except output_file which is STDOUT by default. Result of this script : For each paragraph of the source file given in the command line: %%% TRANSLATION ORIG % original lines % of the paragraph %%% TRANSLATION FR # ready to write here %%% /TRANSLATION """ ########### # FUNCTIONS def handle_line(para, line, converted_lines): """ handle a line in the input. if inside a paragraph, adds it to the current paragraph : para. Else, if terminates the current paragraph (blank line), return the converted paragraph into converted_lines. *para* -- The current paragraph *line* -- the original line, should be a text line without '\n'. *converted_lines* -- the addition of all the converted lines made by this function before. **Warning** : para and converted_lines must be changed in place (with append()). """ # if it's a blank line, ending the paragraph # dump the converted paragraph if BLANK.match(line): if para: # insert header converted_lines.append("%%% TRANSLATION ORIG\n") # insert converted paragraph for lineOfPar in para : lineOfPar = '% ' + lineOfPar lineOfPar = lineOfPar + '\n' converted_lines.append(lineOfPar) # end the current paragraph (changed in place) para[:] = [] # insert footer converted_lines.append("%%% TRANSLATION ") converted_lines.append(LANG_TOKEN) converted_lines.append("\n \n") converted_lines.append("%%% /TRANSLATION\n") # then add back the original blank line line = line + "\n" converted_lines.append(line) # case of a line inside a paragraph # just add it to the current paragraph else: para.append(line) def prepare_para_orig_tex(lines): """ Converts paragraphs (from arg lines) in commented blocks. Return all the converted lines. Use handle_line to treat each line and to create the converted lines text. *lines* -- The whole text to handle """ # current paragraph para = [] # result of conversion of lines of the file converted_lines = [] # header converted_lines.append("%%% TRANSLATION INFO :\n") converted_lines.append("%% Original : release %s\n" % VERSION) converted_lines.append("%% Translation : $%s$\n" % "Id") converted_lines.append("%% Translator : %s\n" % TRANSLATOR) converted_lines.append("%% Language : %s\n\n" % LANG_TOKEN) # loop through the file for line in lines: # handle correctly a file not terminated with \n, not to # truncate last char if line[-1] == '\n' : line = line[:-1] handle_line(para, line, converted_lines) # handle the last paragraph properly (termination of the last # paragraph) # check if the last line is not blank, which means that the last # paragraph needs to be copied on the output. line = lines[-1] if not BLANK.match(line): handle_line(para, '', converted_lines) converted_lines = converted_lines[:-1] return converted_lines def main_parseopts(): """ Handle args given in the command line. Return the file name or exit the script if there is none or if a -h, -v or -? was given. -l or --lang can change the LANG_TOKEN used in the output of the script. """ global LANG_TOKEN global TRANSLATOR global VERSION global OUTPUTFILE # Process command line arguments try: optlist, args = getopt.getopt(sys.argv[1:], 'd?hl:v:t:o:',('lang=','version=','translator=','output_file=','h','help')) except getopt.error, str: sys.stderr.write('prepare_tex: %s\n' % str) sys.stderr.write(__usage__) sys.exit(-1) for opt in optlist: if opt[0] == '-d': print __version__ print print __doc__ sys.exit(0) elif (opt[0] == '-h') or (opt[0] == '--h') or(opt[0] == '--help') \ or (opt[0] == '-?'): print __usage__ sys.exit(0) elif (opt[0] == '-t') or (opt[0]=='--translator'): TRANSLATOR = opt[1] elif (opt[0] == '-o') or (opt[0]=='--output_file'): OUTPUTFILE = opt[1] elif (opt[0] == '-v') or (opt[0]=='--version'): VERSION = opt[1] elif (opt[0] == '-l') or (opt[0]=='--lang'): LANG_TOKEN = opt[1] if not args: sys.stderr.write\ ('\nError: prepare_tex: You must pass at least one Latex file.\n') sys.stderr.write(__usage__) sys.exit(1) else: return args[0] def run(): global TRANSLATOR global VERSION global OUTPUTFILE global LANG_TOKEN inputfile = main_parseopts() if not LANG_TOKEN: LANG_TOKEN = raw_input('Language: [FR] ') if not LANG_TOKEN: LANG_TOKEN = "FR" if not VERSION: VERSION = raw_input\ ('Release of the original on which you apply the translation: [2.0]? ') if not VERSION: VERSION = "2.0" if not TRANSLATOR: TRANSLATOR = raw_input('Your name: ') if not TRANSLATOR: TRANSLATOR = "Unknown" lines = open(inputfile).readlines() # if the file is empty, need to do nothing if lines : res = prepare_para_orig_tex(lines) if not OUTPUTFILE: OUT = sys.stdout else: OUT = open(OUTPUTFILE,'w') for line in res : OUT.write(line) if __name__=='__main__': run() # $Log: prepare_tex.py,v $ # Revision 1.5 2000/10/24 20:25:25 olberger # Typo et correction mineure # # Revision 1.4 2000/10/24 14:36:14 olberger # Ajout version améliorée encore par Régis Leroy # # Revision 1.3 2000/10/23 21:45:32 olberger # Intégration des contributions de Régis Leroy # # Revision 1.1.2.1 2000/10/23 21:00:30 olberger # Intégration contribution Regis Leroy basé sur révision 1.1 # # Revision 1.2 2000/10/23 20:36:08 olberger # Intégration V1.2 d'Odile # # Revision 1.1 2000/10/18 17:45:45 olberger # Ajout scripts pour gestion des commentaires dans les documents traduits #