Debian Codegraph @

src/odf/easyliststyle.py

raw
1 # Create a <text:list-style> element from a text string.
2 # Copyright (C) 2008 J. David Eisenberg
3 #
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 2 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License along
15 # with this program; if not, write to the Free Software Foundation, Inc.,
16 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 #
18 # Contributor(s):
19 #
20
21
22 import re
23
24 from .style import ListLevelProperties
25 from .text import ListLevelStyleBullet, ListLevelStyleNumber, ListStyle
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
46 def styleFromString(name, specifiers, delim, spacing, showAllLevels):
47 specArray = specifiers.split(delim)
48 return styleFromList(name, specArray, spacing, showAllLevels)
49
50
51 def styleFromList(styleName, specArray, spacing, showAllLevels):
52 bullet = ''
53 numPrefix = ''
54 numSuffix = ''
55 cssLengthNum = 0
56 cssLengthUnits = ''
57 numbered = False
58 displayLevels = 0
59 listStyle = ListStyle(name=styleName)
60 numFormatPattern = re.compile(r'([1IiAa])')
61 cssLengthPattern = re.compile(r'([^a-z]+)\s*([a-z]+)?')
62 m = cssLengthPattern.search(spacing)
63 if m is not None:
64 cssLengthNum = float(m.group(1))
65 if m.lastindex == 2:
66 cssLengthUnits = m.group(2)
67 i = 0
68 while i < len(specArray):
69 specification = specArray[i]
70 m = numFormatPattern.search(specification)
71 if m is not None:
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 displayLevels = 1
85 numbered = False
86 if numbered:
87 lls = ListLevelStyleNumber(level=(i + 1))
88 if numPrefix != '':
89 lls.setAttribute('numprefix', numPrefix)
90 if numSuffix != '':
91 lls.setAttribute('numsuffix', numSuffix)
92 lls.setAttribute('displaylevels', displayLevels)
93 else:
94 lls = ListLevelStyleBullet(level=(i + 1), bulletchar=(bullet or '•')[0])
95 llp = ListLevelProperties()
96 llp.setAttribute('spacebefore', str(cssLengthNum * (i + 1)) + cssLengthUnits)
97 llp.setAttribute('minlabelwidth', str(cssLengthNum) + cssLengthUnits)
98 lls.addElement(llp)
99 listStyle.addElement(lls)
100 i += 1
101 return listStyle
102
103
104 # vim: set expandtab sw=4 :