From: Paul Lutus Date: 2006-12-04T05:15:05+09:00 Subject: Re: OOo and regexp ishamid wrote: > [novice] > Hi, > > Paul Lutus suggested that I give more detail about my problem. Ok, > here it is: > > BACKGROUND: > Save a small document in OOo format, Do you mean an Open Office Open Document format? The sort of data file that typically has a suffix of ".odt" and consists of a compressed set of XML files for various purposes? > like a bibliographic entry with, > say, the article title in bold and the journal in italics > - under options - save -> disable xml size optimization > - save the file > - copy the file to a subdirectory > - run "unzip filename" I think this answers my first question. > content.xml has the data we want to convert to TeX. A sample > content.xml is given at the end of this message, after the script. > > RUBY: I have a script provided by a colleague that does a lot of the > work needed to convert this to a sane ConTeXt file. I am trying to > teach myself enough ruby to edit this script as needed for academic > articles (I edit an academic journal in TeX). The script is reproduced > at the end of this message. > > PROBLEMS: Yesterday I did learn about regexp and made progress, though > the script is still buggy: > > i) In the script (l. 110--112) I have > > =========== > str.gsub!(/"(.*?)"/) do > '\quotation {' + $1 + '}' > end > =========== > > but line 114 of content.xml the " pair is not converted, though > it is converted elsewhere. I am unable to correlate this line number with a " sequence in the corresponding line in your provided XML sample. Are the two quote sequences on separate lines? If so, use this form: str.gsub!(/"(.*?)"/m) do '\quotation {' + $1 + '}' end Note the added 'm'. This won't work if you are parsing the file line by line and if the two " sequences are on different XML lines. If (1) you have two " sequences on different lines, and if (2) you are processing the XML content line by line, then you will have to change how you process the file in the most fundamental way to get this particular TeX conversion to work. > > ii) (really weird) In the script (l. 45--47) I have > > ============ > @data.gsub!(/\[<(text:sequence > text:ref-name="refAutoNr0").*?>.*?<\/text:sequence>/mois) do > '\startitemize' + '\head' > end > ============ > > This apparently works fine. Now I want some linespace between > '\startitemize' & '\head', so I put a "\n\n" in between them. This > causes the xml tags to appear in the output file like this > > ============ > \startitemize > > \head > ============ Yes. This is what you instructed the computer to do, and apparently the computer succeeded in meeting your request. I assume this is on the TeX side of the conversion process, and I don't happen to know how a linefeed is represented in TeX, but I believe that (a TeX linefeed) is what you want to insert, not bare linefeeds (unless I have completely misunderstood you). > > iii) any tips for improving this script are appreciated. I'm sure I'll > have more questions over the next couple of days as I work on this. I had hoped for a list of desired conversions, rather than a script that needs work. Most people are reluctant to dig into someone else's code, such an approach normally takes much longer than starting over. I was able to format your XML this time, because the example was complete, and having taken a look at it, I assume this is a OpenOffice Open Document format file, yes? Postscript. Have you considered all your options? OpenOffice will save its documents in many formats, several of which preserve the original formatting. For example, you could save the document as RTF, then use the utility "rtf2TeX" to perform the conversion to TeX. I haven't actually done this, but I can see your effort level and I thought I would alert you to some other options. I would also have mentioned saving as HTML and using html2tex, but I doubt you would be pleased with the outcome (no paging or footnotes AFAIK). Post-postscript. The TeX output is a requirement, yes? There are many excellent output formats that are in wider use today. -- Paul Lutus http://www.arachnoid.com