# ------------------------------------------------------------------------------ # Appy is a framework for building applications in the Python language. # Copyright (C) 2007 Gaetan Delannay # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License # as published by the Free Software Foundation; either version 2 # of the License, or (at your option) any later version. # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,USA. # ------------------------------------------------------------------------------ import re, os.path from UserDict import UserDict import appy.pod from appy.pod import * from appy.pod.odf_parser import OdfEnvironment, OdfParser from appy.shared.css import parseStyleAttribute # Possible states for the parser READING = 0 # Default state PARSING_STYLE = 1 # I am parsing styles definitions # Error-related constants ------------------------------------------------------ MAPPING_NOT_DICT = 'The styles mapping must be a dictionary or a UserDict ' \ 'instance.' MAPPING_ELEM_NOT_STRING = "The styles mapping dictionary's keys and values " \ "must be strings." MAPPING_OUTLINE_DELTA_NOT_INT = 'When specifying "h*" as key in the styles ' \ 'mapping, you must specify an integer as ' \ 'value. This integer, which may be positive ' \ 'or negative, represents a delta that will ' \ 'be added to the html heading\'s outline ' \ 'level for finding an ODT style with the ' \ 'same outline level.' MAPPING_ELEM_EMPTY = 'In your styles mapping, you inserted an empty key ' \ 'and/or value.' UNSTYLABLE_TAG = 'You can\'t associate a style to element "%s". Unstylable ' \ 'elements are: %s' STYLE_NOT_FOUND = 'OpenDocument style "%s" was not found in your template. ' \ 'Note that the styles names ("Heading 1", "Standard"...) ' \ 'that appear when opening your template with OpenOffice, ' \ 'for example, are a super-set of the styles that are really '\ 'recorded into your document. Indeed, only styles that are ' \ 'in use within your template are actually recorded into ' \ 'the document. You may consult the list of available ' \ 'styles programmatically by calling your pod renderer\'s ' \ '"getStyles" method.' HTML_PARA_ODT_TEXT = 'For XHTML element "%s", you must associate a ' \ 'paragraph-wide OpenDocument style. "%s" is a "text" ' \ 'style (that applies to only a chunk of text within a ' \ 'paragraph).' HTML_TEXT_ODT_PARA = 'For XHTML element "%s", you must associate an ' \ 'OpenDocument "text" style (that applies to only a chunk '\ 'of text within a paragraph). "%s" is a paragraph-wide ' \ 'style.' # ------------------------------------------------------------------------------ class Style: '''Represents a paragraph style as found in styles.xml in a ODT file''' numberRex = re.compile('(\d+)(.*)') def __init__(self, name, family): self.name = name self.family = family # May be 'paragraph', etc. self.displayName = name self.styleClass = None # May be 'text', 'list', etc. self.fontSize = None self.fontSizeUnit = None # May be pt, %, ... self.outlineLevel = None # Were the styles lies within styles and # substyles hierarchy def setFontSize(self, fontSize): rexRes = self.numberRex.search(fontSize) self.fontSize = int(rexRes.group(1)) self.fontSizeUnit = rexRes.group(2) def __repr__(self): res = '