ODFPY 1.2.0
 
Loading...
Searching...
No Matches
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
22import re, sys, os.path
23sys.path.append(os.path.dirname(__file__))
24from odf.style import Style, TextProperties, ListLevelProperties
25from odf.text import ListStyle,ListLevelStyleNumber,ListLevelStyleBullet
26
27"""
28Create a <text:list-style> element from a string or array.
29
30List styles require a lot of code to create one level at a time.
31These routines take a string and delimiter, or a list of
32strings, and creates a <text:list-style> element for you.
33Each 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
45_MAX_LIST_LEVEL = 10
46SHOW_ALL_LEVELS = True
47SHOW_ONE_LEVEL = False
48
49def styleFromString(name, specifiers, delim, spacing, showAllLevels):
50 specArray = specifiers.split(delim)
51 return styleFromList( name, specArray, spacing, showAllLevels )
52
53def styleFromList( styleName, specArray, spacing, showAllLevels):
54 bullet = ""
55 numPrefix = ""
56 numSuffix = ""
57 numberFormat = ""
58 cssLengthNum = 0
59 cssLengthUnits = ""
60 numbered = False
61 displayLevels = 0
62 listStyle = ListStyle(name=styleName)
63 numFormatPattern = re.compile("([1IiAa])")
64 cssLengthPattern = re.compile("([^a-z]+)\\s*([a-z]+)?")
65 m = cssLengthPattern.search( spacing )
66 if (m != None):
67 cssLengthNum = float(m.group(1))
68 if (m.lastindex == 2):
69 cssLengthUnits = m.group(2)
70 i = 0
71 while i < len(specArray):
72 specification = specArray[i]
73 m = numFormatPattern.search(specification)
74 if (m != None):
75 numberFormat = m.group(1)
76 numPrefix = specification[0:m.start(1)]
77 numSuffix = specification[m.end(1):]
78 bullet = ""
79 numbered = True
80 if (showAllLevels):
81 displayLevels = i + 1
82 else:
83 displayLevels = 1
84 else: # it's a bullet style
85 bullet = specification
86 numPrefix = ""
87 numSuffix = ""
88 numberFormat = ""
89 displayLevels = 1
90 numbered = False
91 if (numbered):
92 lls = ListLevelStyleNumber(level=(i+1))
93 if (numPrefix != ''):
94 lls.setAttribute('numprefix', numPrefix)
95 if (numSuffix != ''):
96 lls.setAttribute('numsuffix', numSuffix)
97 lls.setAttribute('displaylevels', displayLevels)
98 else:
99 lls = ListLevelStyleBullet(level=(i+1),bulletchar=bullet[0])
100 llp = ListLevelProperties()
101 llp.setAttribute('spacebefore', str(cssLengthNum * (i+1)) + cssLengthUnits)
102 llp.setAttribute('minlabelwidth', str(cssLengthNum) + cssLengthUnits)
103 lls.addElement( llp )
104 listStyle.addElement(lls)
105 i += 1
106 return listStyle
107
108# vim: set expandtab sw=4 :
styleFromString(name, specifiers, delim, spacing, showAllLevels)
styleFromList(styleName, specArray, spacing, showAllLevels)