Use the odfpy api for `Openbaar Schrijver' book.
[openbaar_schrijver.git] / liblove.py
bloba6b047133219501c94b298d03fc6305046f9672b
1 #!/usr/bin/env python
3 """
4 A possible use of odfpy for the `Openbaar Schrijver' book.
5 Written by Open Source Publishing.
7 http://ospublish.constantvzw.org/works/valentine-scripting
8 """
10 import os
12 from odf.text import P
13 from odf.style import Style, TextProperties, ParagraphProperties
14 from odf.style import GraphicProperties
15 from odf.draw import Frame, TextBox
17 # general purpose
18 def directory_structure():
19 """Produces a directory hierarchy for the storage of the ODT's. From the
20 top-level directories and then down, the hierarchy looks like:
22 genre -- one of brief, gedicht, or verhaal
23 style -- relative to genre
24 spice -- one of character, dingbat, or image
26 Each ODT will live inside of a directory named after its spice. cf the
27 `myname' variable of the Spice classes.
29 """
30 genre = ["brief", "gedicht", "verhaal"]
31 spice = ["character", "dingbat", "image"]
32 style = {
33 "brief" : ["oesters", "nevels", "zon", "dromen", "rivieren", "toekan"],
34 "gedicht" : ["vulkanen", "souvenirs", "ster", "golven", "octopus", "rodepepers"],
35 "verhaal" : ["valleien", "ijs", "verlangens", "panter", "maan", "stormen"],
37 # genre = style.keys()
39 for mygenre in genre:
40 try:
41 os.mkdir(mygenre)
42 except:
43 pass
44 for mystyle in style[mygenre]:
45 try:
46 os.mkdir(mygenre + "/" + mystyle)
47 except:
48 pass
49 # myspace
50 for myspice in spice:
51 try:
52 os.mkdir(mygenre + "/" + mystyle + "/" + myspice)
53 except:
54 pass
56 def make_frame(odt, graphic_style, h, w, ax, ay, z):
57 """Creates an ODF frame, and places an ODF textbox inside of it. Returns
58 both the Frame and the TextBox instances. We will often use this function.
59 The arguments are:
61 odt -- an OpenDocumentText instance
62 graphic_style -- a GraphicProperties instance
63 h, w, ax, ay, z -- height, width, x-pos, y-pos and z-index, respectively
65 From the ODF spec: "within text documents, frames are also used to
66 position content outside of the default text flow of a document"
68 """
69 odt.styles.addElement(graphic_style)
70 myframe = Frame(stylename=graphic_style, height=h, width=w,
71 x=ax, y=ay, zindex=z)
72 mytextbox = TextBox()
73 myframe.addElement(mytextbox)
74 return {"frame" : myframe, "textbox" : mytextbox}
76 def kinda_p(paragraphic_style, text, textbox):
77 """Sets the given `text' string in a paragraph inside of `textbox' styled
78 according to `paragraphic_style'.
80 """
81 p = P(stylename=paragraphic_style, text=text)
82 textbox.addElement(p)
84 def frame_border(odt):
85 """Produces the frame around the contents of the ODT documents."""
86 ## graphic
87 graphic_style = Style(name="Border Frame", family="graphic")
88 graphic_properties = GraphicProperties(border="0.5mm double #000000")
89 graphic_style.addElement(graphic_properties)
90 frame = make_frame(odt, graphic_style, "273mm", "194mm", "-12mm", "-12mm", "1")
91 frameframe = frame["frame"]
92 textbox = frame["textbox"]
94 ## paragraphic
95 paragraphic_style = Style(name="Frame Border is Empty", family="paragraph")
96 text_props = TextProperties()
97 paragraphic_style.addElement(text_props)
98 paragraph_props = ParagraphProperties()
99 paragraphic_style.addElement(paragraph_props)
100 odt.styles.addElement(paragraphic_style)
101 kinda_p(paragraphic_style, u"", textbox)
103 odt.text.addElement(frameframe)
105 def header(odt):
106 """Produces the header frame of the `brief' ODT's."""
107 ## graphic
108 #FS ADDED ATLAS STYLE
109 header_graphic_style = Style(name="Header Frame", family="graphic")
110 header_graphic_properties = GraphicProperties(backgroundcolor="#ffffff", border="10mm double #ffffff")
111 header_graphic_style.addElement(header_graphic_properties)
112 frame = make_frame(odt, header_graphic_style, "20mm", "170mm", "0mm", "0mm", "2")
113 frameframe = frame["frame"]
114 textbox = frame["textbox"]
116 ## paragraphic
117 header_paragraphic_style = Style(name="Header - Date", family="paragraph")
118 atlas_paragraphic_style = Style(name="Header - Atlas", family="paragraph")
120 header_text_props = TextProperties(fontsize="12pt", fontfamily="DIN_OSP")
121 atlas_text_props = TextProperties(fontsize="16pt", fontfamily="OSP-Atlast")
123 header_paragraphic_style.addElement(header_text_props)
124 atlas_paragraphic_style.addElement(atlas_text_props)
126 header_paragraph_props = ParagraphProperties(textalign="right")
127 atlas_paragraph_props = ParagraphProperties(textalign="right")
128 header_paragraphic_style.addElement(header_paragraph_props)
129 atlas_paragraphic_style.addElement(atlas_paragraph_props)
130 odt.styles.addElement(header_paragraphic_style)
131 odt.styles.addElement(atlas_paragraphic_style)
133 kinda_p(header_paragraphic_style, u"Gaasbeek, 13 februari 2050", textbox)
135 kinda_p(atlas_paragraphic_style, u"Hello World", textbox)
137 odt.text.addElement(frameframe)