ODFPY  1.2.0
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 odf.style import Style, TextProperties, ListLevelProperties
25 from odf.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 based on the
39  * first character in the item.</p>
40 """
41 
42 _MAX_LIST_LEVEL = 10
43 SHOW_ALL_LEVELS = True
44 SHOW_ONE_LEVEL = False
45 
46 def styleFromString(name, specifiers, delim, spacing, showAllLevels):
47  specArray = specifiers.split(delim)
48  return styleFromList( name, specArray, spacing, showAllLevels )
49 
50 def styleFromList( styleName, specArray, spacing, showAllLevels):
51  bullet = ""
52  numPrefix = ""
53  numSuffix = ""
54  numberFormat = ""
55  cssLengthNum = 0
56  cssLengthUnits = ""
57  numbered = False
58  displayLevels = 0
59  listStyle = ListStyle(name=styleName)
60  numFormatPattern = re.compile("([1IiAa])")
61  cssLengthPattern = re.compile("([^a-z]+)\\s*([a-z]+)?")
62  m = cssLengthPattern.search( spacing )
63  if (m != 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 != None):
72  numberFormat = m.group(1)
73  numPrefix = specification[0:m.start(1)]
74  numSuffix = specification[m.end(1):]
75  bullet = ""
76  numbered = True
77  if (showAllLevels):
78  displayLevels = i + 1
79  else:
80  displayLevels = 1
81  else: # it's a bullet style
82  bullet = specification
83  numPrefix = ""
84  numSuffix = ""
85  numberFormat = ""
86  displayLevels = 1
87  numbered = False
88  if (numbered):
89  lls = ListLevelStyleNumber(level=(i+1))
90  if (numPrefix != ''):
91  lls.setAttribute('numprefix', numPrefix)
92  if (numSuffix != ''):
93  lls.setAttribute('numsuffix', numSuffix)
94  lls.setAttribute('displaylevels', displayLevels)
95  else:
96  lls = ListLevelStyleBullet(level=(i+1),bulletchar=bullet[0])
97  llp = ListLevelProperties()
98  llp.setAttribute('spacebefore', str(cssLengthNum * (i+1)) + cssLengthUnits)
99  llp.setAttribute('minlabelwidth', str(cssLengthNum) + cssLengthUnits)
100  lls.addElement( llp )
101  listStyle.addElement(lls)
102  i += 1
103  return listStyle
104 
105 # vim: set expandtab sw=4 :
def ListLevelStyleBullet(args)
Definition: text.py:256
def ListLevelStyleNumber(args)
Definition: text.py:262
def ListStyle(args)
Definition: text.py:265
def styleFromList(styleName, specArray, spacing, showAllLevels)
def styleFromString(name, specifiers, delim, spacing, showAllLevels)
Definition: text.py:1
def ListLevelProperties(args)
Definition: style.py:95