Fix CSS for margins in definition lists (html5).
[docutils.git] / sandbox / stylesheets / pygments_css2sty.py
blob185050b7a8ebdfb5e63b931986c0dd6b1058a2fe
1 #! /usr/bin/env python
2 # coding: utf8
3 # Copyright: Raphael 'kena' Poss <r.c.poss@uva.nl>
4 # this file is placed in the public domain.
6 # Convert a CSS stylesheet into one for Docutils' LaTeX output.
8 # Usage example::
10 # pygmentize -S default -f html | pygments_css2sty.py >pygments-default.sty
12 # Versions:
14 # 2012-05-09: Günter Milde <milde@users.sf.net>:
15 # Bugfix: do not fail at lines without comment.
16 # Support for digits in role names.
17 # ``\providecommand`` instead of ``\newcommand``.
18 # Renamed from makesty.py to pygments_css2sty.py.
19 # 2013-03-27: Günter Milde:
20 # Implement bugfix from Juan Luis Cano Rodríguez for csnames.
22 import sys
23 import re
25 print '% Stylesheet for syntax highlight with Docutils'
26 print '% Generated by pygments_css2sty.py from a Pygments CSS style'
27 print '% (output of `pygmentize -S <style> -f html`).'
28 print
29 print r'\RequirePackage{color}'
31 cnt = 0
32 for l in sys.stdin:
34 if '/*' in l:
35 print "% " + l.split('*')[1]
36 key = l.split(' ', 1)[0][1:]
38 s = '#1'
40 if 'color:' in l:
41 col = l.split('#',1)[1][:6]
42 r = float(int(col[0:2], 16)) / 255.
43 g = float(int(col[2:4], 16)) / 255.
44 b = float(int(col[4:6], 16)) / 255.
45 s = r'\textcolor[rgb]{%.2f,%.2f,%.2f}{%s}' % (r, g, b, s)
47 if 'font-style: italic' in l:
48 s = r'\textit{%s}' % s
49 if 'font-weight: bold' in l:
50 s = r'\textbf{%s}' % s
52 if 'border:' in l:
53 col = l.split('#',1)[1][:6]
54 r = float(int(col[0:2], 16)) / 255.
55 g = float(int(col[2:4], 16)) / 255.
56 b = float(int(col[4:6], 16)) / 255.
57 cname = 'DUcolor%d' % cnt
58 cnt += 1
59 print r'\definecolor{%s}{rgb}{%.2f,%.2f,%.2f}' % (cname, r, g, b)
60 s = r'\colorbox{%s}{%s}' % (cname, s)
62 if re.match(r'.*[0-9]', key) is None:
63 print(r'\providecommand*\DUrole%s[1]{%s}' % (key, s))
64 else:
65 print(r'\expandafter\providecommand\csname DUrole%s\endcsname[1]{%s}'
66 % (key, s))