José Matos Thu, 31 May 2007 08:26:07 -0700 On Thursday 31 May 2007 10:08:31 Anders Ekberg wrote: > Hi! > > Attached find a patch that aims to (at least partly) solve bug 3313. > Currently, an attempt to export a document containing any unicode> character (for example an en dash) to LyX1.4 results in an error message> "An error occurred whilst running python -tt" > The attached function (enclosed as a tect file), intended to be > included in lib/lyx2lyx/lyx_1_5.py, tries to solve this by reading > the unicodesymbols file and replacing symbols in that file with > corresponding commands. > > More details, description of the limitations etc, are on > http://bugzilla.lyx.org/show_bug.cgi?id=3313 > together with a test case (note that Uwe's test file converts but > does not generate a pdf since preamble is not added by the function; > this is a deliberate choice that can be discussed). > > Note that this is my first encounter with any LyX-code. Also, i am > not used to this types of large-scale codes, so there may well > stupidities in the code. Still, it seems to work well (at least for > the test cases) and I think it improves the current situation.You are right, great work. :-) Could you send a patch, please? It is easier to read.
Thanks! Patch enclosed.
> Anders Bellow I send some comments to your code. :-) > revert_unicode_070530.txt > import sys, os > > ... > > def revert_unicode(document): > r'''Transform unicode symbols according to the unicode list. > Preamble flags are not implemented. > Combination characters are currently ignored. > Forced output is currently not enforced'''No need to start the string by r, the only use of raw strings in python is to avoid the escape of backslashes. Your documentation above has none. :-)
OK (bad imitation from my part ;-)
I don't think that works since the unicodesymbols file is in the parent directory (at least in the Mac version). fp = open('../ unicodesymbols') didn't work for me since the working directory is some kind of temp-directory.> pathname = os.path.dirname(sys.argv[0]); > try: > fp = open(pathname.strip('lyx2lyx') + 'unicodesymbols','r'); > except: > return;Why do you need such a complicated statement? What is wrong with simplyfp = open('unicodesymbols')
I skipped the try/except part (I guess if the unicodesymbols file is missing, a failure in the export to 1.4 is probably the small problem... ;-)
And please remove all semi-commas from the end of lines they are unneeded andunpythonic. :-)
Done (bad Matlab habits ;-)
> spec_chars = {}; > for line in fp.readlines(): > if line[0] != '#': > line=line.replace('"',''); #remove all qoutation marks > try: > # flag1 and flag2 are preamble & flags > # currently NOT impemented > [ucs4,command,flag1,flag2] =line.split(None,3);> spec_chars[unichr(eval(ucs4))] = [command, flag1, flag2];> except: > pass > fp.close(); > #Define strings to start and end ERT and math insets> ert_intro='\n\n\\begin_inset ERT\nstatus collapsed\n\ \begin_layout > Standard\n\\backslash\n'; ert_outro='\n\\end_layout\n\n\\end_inset \n\n';> math_intro='\n\\begin_inset Formula $' > math_outro='$\n\\end_inset\n' > # Find unicode characters and replace them > in_ert = 0; # flag set to 1 if in ERT inset > in_math = 0; # flag set to 1 if in math inset > insets = []; # list of active insets > for i in range(len(document.body)): > current_line = document.body[i]; Those two lines can be condensed into one: for i, current_line in enumerate(document.body):
Nice! Thanks for the help! Anders
patch
Description: Binary data