File: tohtml

package info (click to toggle)
songwrite 0.12b-4
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 984 kB
  • ctags: 989
  • sloc: python: 6,321; xml: 850; makefile: 10
file content (69 lines) | stat: -rwxr-xr-x 1,730 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#! /usr/bin/python
# -*- python -*-

# Copyright (C) 2001-2002 Jean-Baptiste LAMY
#
# This program is free software. See README or LICENSE for the license terms.

# tohtml <lyx file>
# Exports the lyx file to HTML, and keeps the JPEG / PNG pictures.

image_formats = (".png", ".jpeg")

import os, os.path, sys, re, tempfile

def do(command):
  print command
  os.system(command)
  print
  
lyx_file         = sys.argv[1]
latex_file       = lyx_file[:-4] + ".tex"
hevea_image_file = lyx_file[:-4] + ".image.tex"
html_file        = lyx_file[:-4] + ".html"
base             = os.path.dirname(lyx_file)

already_existant_eps = filter(lambda file: file.endswith(".eps"), os.listdir(base))


do("lyx -e latex %s" % lyx_file)

latex = open(latex_file).read()

NoImageError = "NoImageError"

def repl(match):
  image_file = match.group(1)
  image_file = image_file[:image_file.rfind(".")]
  
  for image_format in image_formats:
    filename = image_file + image_format
    if (os.path.isabs(filename) and os.path.exists(filename)) or os.path.exists(os.path.join(base, filename)):
      return r"\includegraphics{%s}" % filename
    
  else: raise NoImageError, image_file
  
latex = re.sub(r"\\includegraphics.*?{(.*?)}", repl, latex)

open(latex_file, "w").write(latex)

macro_file = tempfile.mktemp(".hva")

open(macro_file, "w").write(r"""
\renewcommand{\heveaimageext}{.png}
\newcommand{\includegraphics}[1]{\imgsrc{#1}}
""")

do("hevea -o %s %s %s" % (html_file, macro_file, latex_file))

os.remove(macro_file)
os.remove(latex_file)


#Remove EPS file created by LyX

for file in os.listdir(base):
  if file.endswith(".eps") and not (file in already_existant_eps): os.remove(os.path.join(base, file))

#os.remove(hevea_image_file)