I'm interested in adding a new graphics format (Xournal) to LyX that
can be displayed on-screen and rendered in PDF outputs. Just to get
started, I've been trying to figure out how to add a dummy graphics
format. The input is actually a .tex file (but named .foo), and I
tried adding a converter to PDF, but not luck. Details follow.
In Tools > Preferences, I added a new format with:
Format: foo
Short Name: foo
Extension: foo
Editor: gedit
Viewer: gedit
I also added a new converter to PDF (ps2pdf) with:
pdflatex -outputdirectory /tmp/ /home/yang/blah/blah.foo && cp /tmp/blah.pdf $$o
Note I'm ignoring $$i, just for the sake of getting *something* working.
Now I tried inserting a simple hello world latex file named .foo into
my document as a graphic. With lyx -dbg graphics, I see the following
output, and in the main window I get the error "Error converting to
loadable format." Any hints? Thanks in advance.
../../src/graphics/GraphicsCacheItem.cpp(409): Converting it to format.
../../../src/support/FileName.cpp(818): filetools(getFormatFromContents)
File type not recognised before EOF!
../../../src/support/FileName.cpp(935): filetools(getFormatFromContents)
Couldn't find a known format!
../../src/graphics/GraphicsCacheItem.cpp(379):
[CacheItem::Impl::convertToDisplayFormat]
Attempting to convert image file: /home/yang/blah/blah.foo
with displayed filename: ~/blah/blah.foo
../../../src/support/FileName.cpp(818): filetools(getFormatFromContents)
File type not recognised before EOF!
../../../src/support/FileName.cpp(935): filetools(getFormatFromContents)
Couldn't find a known format!
../../src/Format.cpp(146): will guess format from file extension: foo ->
foo
../../src/graphics/GraphicsCacheItem.cpp(386):
The file contains foo format data.
../../src/graphics/GraphicsCacheItem.cpp(340): Unable to convert from foo to bmp
../../src/graphics/GraphicsCacheItem.cpp(340): Unable to convert from foo to gif
../../src/graphics/GraphicsCacheItem.cpp(340): Unable to convert from foo to jpg
../../src/graphics/GraphicsCacheItem.cpp(340): Unable to convert from foo to jpg
../../src/graphics/GraphicsCacheItem.cpp(340): Unable to convert from foo to pbm
../../src/graphics/GraphicsCacheItem.cpp(340): Unable to convert from foo to pgm
../../src/graphics/GraphicsCacheItem.cpp(340): Unable to convert from foo to png
../../src/graphics/GraphicsCacheItem.cpp(340): Unable to convert from foo to ppm
../../src/graphics/GraphicsCacheItem.cpp(340): Unable to convert from
foo to tiff
../../src/graphics/GraphicsCacheItem.cpp(340): Unable to convert from foo to xbm
../../src/graphics/GraphicsCacheItem.cpp(340): Unable to convert from foo to xpm
../../src/graphics/GraphicsConverter.cpp(127): Converter c-tor:
from_file: /home/yang/blah/blah.foo
to_file_base: /tmp/lyx_tmpdir.J18704/CacheItem.J18704
from_format: foo
to_format: ppm
../../src/graphics/GraphicsConverter.cpp(262): build_script ...
../../src/graphics/GraphicsConverter.cpp(342): No converter defined!
I use convertDefault.py
python -tt "/usr/share/lyx/scripts/convertDefault.py" foo:' + '"' +
infile + '"' + ' ppm:' + '"' + outfile + '"' + '
../../src/graphics/GraphicsConverter.cpp(385): ready!
../../src/graphics/GraphicsConverter.cpp(140): Conversion script:
--------------------------------------
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os, shutil, sys, locale
def unlinkNoThrow(file):
''' remove a file, do not throw if an error occurs '''
try:
os.unlink(file)
except:
pass
def utf8ToDefaultEncoding(file):
''' if possible, convert to the default encoding '''
try:
language, output_encoding = locale.getdefaultlocale()
if output_encoding == None:
output_encoding = 'latin1'
return unicode(file, 'utf8').encode(output_encoding)
except:
return file
infile = utf8ToDefaultEncoding("/home/yang/blah/blah.foo")
outfile = utf8ToDefaultEncoding("/tmp/lyx_tmpdir.J18704/gconvert2.y18704.foo")
shutil.copy(infile, outfile)
os.chdir(utf8ToDefaultEncoding('/tmp/lyx_tmpdir.J18704/'))
infile = outfile
outfile = utf8ToDefaultEncoding("/tmp/lyx_tmpdir.J18704/CacheItem.J18704.ppm")
if os.system(r'python -tt "/usr/share/lyx/scripts/convertDefault.py"
foo:' + '"' + infile + '"' + ' ppm:' + '"' + outfile + '"' + '') != 0:
unlinkNoThrow(outfile)
sys.exit(1)
if not os.path.isfile(outfile):
if os.path.isfile(outfile + '.0'):
os.rename(outfile + '.0', outfile)
import glob
for file in glob.glob(outfile + '.?'):
unlinkNoThrow(file)
else:
sys.exit(1)
unlinkNoThrow(infile)
fromfile = utf8ToDefaultEncoding(outfile)
tofile = utf8ToDefaultEncoding("/tmp/lyx_tmpdir.J18704/CacheItem.J18704.ppm")
try:
os.rename(fromfile, tofile)
except:
try:
shutil.copy(fromfile, tofile)
except:
sys.exit(1)
unlinkNoThrow(fromfile)
--------------------------------------
../../../src/support/ForkedCalls.cpp(465): ForkedCallQueue: waking up
convert: unable to open image
`foo:/tmp/lyx_tmpdir.J18704/gconvert2.y18704.foo': No such file or
directory @ blob.c/OpenBlob/2480.
convert: missing an image filename
`ppm:/tmp/lyx_tmpdir.J18704/CacheItem.J18704.ppm' @
convert.c/ConvertImageCommand/2838.
/usr/share/lyx/scripts/convertDefault.py ERROR
Execution of "convert" failed.
../../../src/support/ForkedCalls.cpp(474): ForkedCallQueue: I'm going to sleep
../../src/graphics/GraphicsCacheItem.cpp(263): Image conversion failed.
../../src/graphics/GraphicsCacheItem.cpp(273): Unable to find converted file!
yang@fl:~/blah$ tail ~/.lyx/preferences
#
# CONVERTERS SECTION ##########################
#
\converter "foo" "pdf" "pdflatex -outputdirectory /tmp/
/home/yang/blah/blah.foo && cp /tmp/blah.pdf $$o" ""
#
# COPIERS SECTION ##########################
#
--
Yang Zhang
http://yz.mit.edu/