Jose' Matos wrote:
>> Could someone write the 5 lines of code? Pleeeease?
>
> def convert_external(lines):
[snip...]
Thanks José. Thanks Dekel.
I amalgamated your suggestions. Attached.
It works fine, but I have a couple of (three) questions:
1. Can I add the header variable into the expression searched by the regex
engine? Rather than write it out again. Would be more elegant.
2. In sed I would write "(.*)" as "\([^"]*\)". Ie include everthing that is
not a '"' rather than include everything.
It is much more efficient in sed. Is the same not true in python? If so,
what is the syntax?
3. Ideally I would not output the filename if it is empty (as occurs with
the Date template. How to do that?
def convert_external(lines):
header="\\begin_inset External"
i = 0
while 1:
i = find_token(lines, header, i)
if i == -1:
break
mo = re.search(r'\\begin_inset External (.*),"(.*)",', lines[i])
if mo:
template = "\ttemplate " + mo.group(1)
filename = "\tfilename \"" + mo.group(2) + "\""
lines[i:i+1] = [header, template, filename]
i = i + 1
--
Angus
? lib/reLyX-safe
Index: lib/ChangeLog
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/lib/ChangeLog,v
retrieving revision 1.447
diff -u -p -r1.447 ChangeLog
--- lib/ChangeLog 4 Jun 2003 09:16:28 -0000 1.447
+++ lib/ChangeLog 4 Jun 2003 11:21:04 -0000
@@ -1,3 +1,9 @@
+2003-06-04 Angus Leeming <[EMAIL PROTECTED]>
+
+ * lyx2lyx/lyx2lyx: bump the output format to 224.
+ * lyx2lyx/lyxconvert_221.py (convert_external): new function.
+ An amalgamation of suggestions from José Matos and Dekel Tsur.
+
2003-06-03 Angus Leeming <[EMAIL PROTECTED]>
* external_templates: modify the templates to use the converter" mechanism.
Index: lib/lyx2lyx/lyx2lyx
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/lib/lyx2lyx/lyx2lyx,v
retrieving revision 1.8
diff -u -p -r1.8 lyx2lyx
--- lib/lyx2lyx/lyx2lyx 14 Mar 2003 13:37:34 -0000 1.8
+++ lib/lyx2lyx/lyx2lyx 4 Jun 2003 11:21:06 -0000
@@ -37,7 +37,7 @@ opt.quiet = 0
format = re.compile(r"(\d)[\.,]?(\d\d)")
fileformat = re.compile(r"\\lyxformat\s*(\S*)")
-lst_ft = ["210", "215", "216", "217", "218", "220", "221", "223"]
+lst_ft = ["210", "215", "216", "217", "218", "220", "221", "224"]
def usage():
print """Usage: lyx2lyx [options] file1
Index: lib/lyx2lyx/lyxconvert_221.py
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/lib/lyx2lyx/lyxconvert_221.py,v
retrieving revision 1.5
diff -u -p -r1.5 lyxconvert_221.py
--- lib/lyx2lyx/lyxconvert_221.py 2 Jun 2003 14:16:17 -0000 1.5
+++ lib/lyx2lyx/lyxconvert_221.py 4 Jun 2003 11:21:06 -0000
@@ -16,6 +16,7 @@
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
import string
+import re
from parser_tools import find_token
def add_end(header):
@@ -38,11 +39,26 @@ def remove_insetparent(lines):
break
del lines[i:i+3]
+def convert_external(lines):
+ header="\\begin_inset External"
+ i = 0
+ while 1:
+ i = find_token(lines, header, i)
+ if i == -1:
+ break
+ mo = re.search(r'\\begin_inset External (.*),"(.*)",', lines[i])
+ if mo:
+ template = "\ttemplate " + mo.group(1)
+ filename = "\tfilename \"" + mo.group(2) + "\""
+ lines[i:i+1] = [header, template, filename]
+ i = i + 1
+
def convert(header, body):
add_end(header)
convert_spaces(body)
convert_bibtex(body)
remove_insetparent(body)
+ convert_external(body)
if __name__ == "__main__":
pass