ODFPY: odf/easyliststyle.py Source File

ODFPY 1.2.0 |

All Classes Namespaces Files Functions Variables
easyliststyle.py
Go to the documentation of this file.
1 # -*- coding: utf-8 -*-
2 # Create a <text:list-style> element from a text string.
3 # Copyright (C) 2008 J. David Eisenberg
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License along
16 # with this program; if not, write to the Free Software Foundation, Inc.,
17 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 #
19 # Contributor(s):
20 #
21
22 import re, sys, os.path
23 sys.path.append(os.path.dirname(__file__))
24 from style import Style, TextProperties, ListLevelProperties
25 from text import ListStyle,ListLevelStyleNumber,ListLevelStyleBullet
26
27 """
28 Create a <text:list-style> element from a string or array.
29
30 List styles require a lot of code to create one level at a time.
31 These routines take a string and delimiter, or a list of
32 strings, and creates a <text:list-style> element for you.
33 Each item in the string (or array) represents a list level
34 * style for levels 1-10.</p>
35 *
36 * <p>If an item contains <code>1</code>, <code>I</code>,
37 * <code>i</code>, <code>A</code>, or <code>a</code>, then it is presumed
38 * to be a numbering style; otherwise it is a bulleted style.</p>
39 """
40
41 _MAX_LIST_LEVEL = 10
42 SHOW_ALL_LEVELS = True
43 SHOW_ONE_LEVEL = False
44
45 def styleFromString(name, specifiers, delim, spacing, showAllLevels):
46 specArray = specifiers.split(delim)
47 return styleFromList( name, specArray, spacing, showAllLevels )
48
49 def styleFromList( styleName, specArray, spacing, showAllLevels):
50 bullet = ""
51 numPrefix = ""
52 numSuffix = ""
53 numberFormat = ""
54 cssLengthNum = 0
55 cssLengthUnits = ""
56 numbered = False
57 displayLevels = 0
58 listStyle = ListStyle(name=styleName)
59 numFormatPattern = re.compile("([1IiAa])")
60 cssLengthPattern = re.compile("([^a-z]+)\\s*([a-z]+)?")
61 m = cssLengthPattern.search( spacing )
62 if (m != None):
63 cssLengthNum = float(m.group(1))
64 if (m.lastindex == 2):
65 cssLengthUnits = m.group(2)
66 i = 0
67 while i < len(specArray):
68 specification = specArray[i]
69 m = numFormatPattern.search(specification)
70 if (m != None):
71 numberFormat = m.group(1)
72 numPrefix = specification[0:m.start(1)]
73 numSuffix = specification[m.end(1):]
74 bullet = ""
75 numbered = True
76 if (showAllLevels):
77 displayLevels = i + 1
78 else:
79 displayLevels = 1
80 else: # it's a bullet style
81 bullet = specification
82 numPrefix = ""
83 numSuffix = ""
84 numberFormat = ""
85 displayLevels = 1
86 numbered = False
87 if (numbered):
88 lls = ListLevelStyleNumber(level=(i+1))
89 if (numPrefix != ''):
90 lls.setAttribute('numprefix', numPrefix)
91 if (numSuffix != ''):
92 lls.setAttribute('numsuffix', numSuffix)
93 lls.setAttribute('displaylevels', displayLevels)
94 else:
95 lls = ListLevelStyleBullet(level=(i+1),bulletchar=bullet[0])
96 llp = ListLevelProperties()
97 llp.setAttribute('spacebefore', str(cssLengthNum * (i+1)) + cssLengthUnits)
98 llp.setAttribute('minlabelwidth', str(cssLengthNum) + cssLengthUnits)
99 lls.addElement( llp )
100 listStyle.addElement(lls)
101 i += 1
102 return listStyle
103
104 # vim: set expandtab sw=4 :
odf.easyliststyle.styleFromString
def styleFromString
Definition: easyliststyle.py:45
odf.style.ListLevelProperties
def ListLevelProperties
Definition: style.py:95
odf.text.ListStyle
def ListStyle
Definition: text.py:265
odf.easyliststyle.styleFromList
def styleFromList
Definition: easyliststyle.py:49
odf.text.ListLevelStyleNumber
def ListLevelStyleNumber
Definition: text.py:262
odf.text.ListLevelStyleBullet
def ListLevelStyleBullet
Definition: text.py:256