From: ishamid Date: 2006-12-04T02:00:20+09:00 Subject: OOo and regexp [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, 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" 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. 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 ============ 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. Thank you all in advance for any help or pointers for this novice :-) Best Idris ================idris.rb============== class OpenOffice # using an xml parser if overkill and we need to regexp anyway attr_reader :display, :inline, :translate attr_writer :display, :inline, :translate def initialize @data = nil @file = '' @display = Hash.new @inline = Hash.new @translate = Hash.new end def load(filename) if not filename.empty? and FileTest.file?(filename) then begin @data, @file = IO.read(filename), filename rescue @data, @file = nil, '' end else @data, @file = nil, '' end end def save(filename='') if filename.empty? then filename = "clean-#{@file}.tex" end if f = open(filename,'w') then f.puts(@data) f.close end end def convert @translations = Hash.new @translate.each do |k,v| @translations[/#{k}/] = v end if @data then @data.gsub!(/\[<(text:sequence text:ref-name="refAutoNr0").*?>.*?<\/text:sequence>/mois) do '\startitemize' + "\n\n" + '\head' # + "\n\n" end @data.gsub!(/\[<\/(text:span)><(text:sequence text:ref-name="refAutoNr[^0].*?").*?>.*?<\/text:sequence>/mois) do '\head' end @data.gsub!(/\[<(text:sequence text:ref-name="refAutoNr[^0].*?").*?>.*?<\/text:sequence>/mois) do '\head' end @data.gsub!(/.*?<(office:text).*?>(.*?)<\/\1>.*/mois) do '\enableregime[utf]' + "\n" + '\useencoding[cyr]' + "\n\n" + '\definetypeface [russian]'\ + "\n" + ' ' + '[rm] [serif] [computer-modern] [default] [encoding=t2a]' + "\n\n" +\ '\starttext'+ "\n\n" + '\switchtobodyfont[russian]' + "\n" + $2 + "\n" + \ '\stopitemize' + "\n\n" + '\stoptext' end @data.gsub!(/<(office:font-face-decls|office:automatic-styles|text:sequence-decls).*?>.*?<\/\1>/mois) do # remove end # @data.gsub!(/<(text:span text:style-name="T10")>(.*?)<\/text:span>/mois) do # '{' + '\bf ' + $2 + '}' # end # @data.gsub!(/<(text:span text:style-name="Style2")>(.*?)<\/text:span>/mois) do # '{' + '\it ' + $2 + '}' # end @data.gsub!(/(.*?)<\/text:span>/) do tag, text = $2, $3 if inline[tag] then (inline[tag][0]||'') + clean_display(text) + (inline[tag][1]||'') else clean_display(text) end end @data.gsub!(//) do # remove end @data.gsub!(/<\?.*?\?>/) do # remove end @data.gsub!(//) do # remove end @data.gsub!(/]*?\/>/) do # remove end @data.gsub!(/(.*?)<\/text:p>/) do tag, text = $2, $3 if display[tag] then "\n" + (display[tag][0]||'') + clean_inline(text) + (display[tag][1]||'') + "\n" else "\n" + clean_inline(text) + "\n" end end @data.gsub!(/]*?\/>/) do # remove end @data.gsub!(/]*?\/>/) do # remove end @data.gsub!(/\t/,' ') @data.gsub!(/^ +$/,'') @data.gsub!(/\n\n+/moi,"\n\n") end end def clean_display(str) str.gsub!(/"(.*?)"/) do '\quotation {' + $1 + '}' end str.gsub!(/&/) do '\&' end str end def clean_inline(str) @translations.each do |k,v| str.gsub!(k,v) end str end end def convert(filename) doc = OpenOffice.new doc.display['P1'] = ['\chapter{','}'] doc.display['P2'] = ['\start'+"\n","\n"+'\stop'] doc.display['P3'] = doc.display['P2'] # doc.display['ID'] = ['\relax'] doc.inline['T1'] = ['',''] doc.inline['T2'] = ['',''] doc.inline['T3'] = ['{\bf ','}'] doc.inline['T6'] = ['{\bf ','}'] doc.inline['T8'] = ['{\bf ','}'] doc.inline['T10'] = ['{\bf ','}'] doc.inline['T11'] = ['{\bf ','}'] doc.inline['Style2'] = ['{\it ','}'] # doc.translate['��'] = 'XX' doc.translate['''] = '`' doc.translate['&'] = '\&' doc.load(filename) doc.convert doc.save end filename = ARGV[0] filename = 'content.xml' if not filename or filename.empty? convert('content.xml') ===========content.xml============ Isma'ilis: A Bibliography Compiled by: Ramin Khanbagi [1 'Abd al-R����ziq, Ahmad Die al-Azhar-Moschee., in, "Sch����tze der Kalifen: Islamische Kunst zur Fatimidenzeit.", Herausgegeben von W. Seipel, Vienna: Kunsthistorisches Museum Wien; Milan: Skira, 1998, pp. 144-147 [2 'Abd al-R����ziq, Ahmad La mosqu����e al-Azhar., in, "Tr����sors fatimides du Caire. Exposition pr����sent����e ���� l'Institut du Monde Arabe ... 1998.", Paris: Institut du Monde Arabe, 1998, pp. 147-149 [3 'Amri, Husay 'Abdallah The Text of an Unpublished Fatwa of the Scholar al-Maqbali (d. 1108/1728) Concerning the Legal Position of the Batiniyyah (Isma'iliyyah) of the People of Hamdan., Translated by A.B.D.R. Eagle, New Arabian Studies, 2 (1994), pp. 165-174. [4 Abarahamov, Binyamin An Isma'ili Epistemology: The Case of Al-Da'i al-Mutlaq 'Ali b. Muhammad b. al-Walid., Journal of Semitic Studies, 41ii (1996), pp. 263-273. [5 Abel, A. De historische betekenis van de Loutere Broeders van Basra (Bassorah), een wijsgerig gezelschap in de Islam van de Xe eeuw., Orientalia Gandensia, 1 (1964), pp. 157-170. [6 Abou Said, A.C. Abbasid and Fatimid Political Relations during the Buhawid Period., University of Cambridge, 1967. [Dissertation] [7 Abu Firas, Shihab al-Din al-Maynaqi Ash-Shafiya': An Isma'ili Treatise., Edited and Translated with an Introduction and Commentary by Sami Nasib Makarim, Beirut: American University of Beirut, 1966. [8 Abu'l-Fida, al-Malik al-Mu'ayyad 'Imad al-Din Ismai'l b. 'Ali The Memoirs of a Syrian Prince., Translated by Peter Malcom Holt, Wiesbaden: Franz Steiner Verlag, [Freiburger Islamstudien], 1983. [9 Abu-Lughod, J. Cairo: 1001 Years of the City Victorious., Princeton: Princeton University Press, 1971. [10 Adamji, Ebrahimji N. and Sorabji M. Darookhanawala Two Indian Travellers: East Africa, 1902-1905: Being Accounts of Journeys Made by Ebrahimji N. Adamji, a Very Young Bohra Merchant from Mombasa & Sorabji M. Darookhanawala, a Middle-Aged Parsi Engineer from Zanzibar., Edited by C. Salvadori and J. Aldrick, Mombasa: Friends of Fort Jesus, 1997. [11 �����������������������������������������,�������������������������� ��������������������������������������������������� ���������������������������������������������������������������������������������������������������������������(������������������������������������������������������������������������������������������������������������������������������������). (Summary: The Religious Situation on the Pamirs (to the problem of religious syncretism).)., ��������������������������, 2000 vi, pp. 36-49;219 [Ismailis in Tajikistan.] [12 ��������������������������������������,����������������������������������������� ������������������������������������: ���������������������������������������������������������������������������������������������., ������������������������������������������������������������������������������������������, 2000ii/8, pp. 128-138 [Also online at www.ca-c.org/journal-table-rus.shtml]