diff options
author | Martin Vilcans <martin@librador.com> | 2013-08-13 20:06:16 +0200 |
---|---|---|
committer | Martin Vilcans <martin@librador.com> | 2013-08-13 20:06:16 +0200 |
commit | 82278cc04327eafb62793d4b6d8b67899672164b (patch) | |
tree | 78d28793fe31ea7b0d1dd4debc84ab4c811bc42e | |
parent | df008aca56897bbb96272a730fe259d0e9b98996 (diff) | |
download | screenplain-82278cc04327eafb62793d4b6d8b67899672164b.tar.gz |
Always output styles in consistent order (Bold, Italic, Underline)
Keep it independent of Python's set implementation.
-rw-r--r-- | screenplain/export/fdx.py | 2 | ||||
-rw-r--r-- | screenplain/richstring.py | 8 |
2 files changed, 7 insertions, 3 deletions
diff --git a/screenplain/export/fdx.py b/screenplain/export/fdx.py index 0f92cf9..85cc0ab 100644 --- a/screenplain/export/fdx.py +++ b/screenplain/export/fdx.py @@ -30,7 +30,7 @@ def _write_text_element(out, styles, text): def write_text(out, rich, trailing_linebreak): """Writes <Text Style="..."> elements.""" for seg_no, segment in enumerate(rich.segments): - fdx_styles = set(style_names[n] for n in segment.styles) + fdx_styles = [style_names[n] for n in segment.get_ordered_styles()] if trailing_linebreak and seg_no == len(rich.segments) - 1: _write_text_element(out, fdx_styles, segment.text + '\n') else: diff --git a/screenplain/richstring.py b/screenplain/richstring.py index c602966..43f5949 100644 --- a/screenplain/richstring.py +++ b/screenplain/richstring.py @@ -81,7 +81,7 @@ class Segment(object): def __repr__(self): return '(%s)(%r)' % ( '+'.join( - style.name() for style in self.styles + style.name() for style in self.get_ordered_styles() ) or 'plain', self.text ) @@ -101,8 +101,12 @@ class Segment(object): self.text != other.text or self.styles != other.styles ) + def get_ordered_styles(self): + """Get the styles in this segment in a deterministic order.""" + return [style for style in all_styles if style in self.styles] + def to_html(self): - ordered_styles = list(self.styles) + ordered_styles = self.get_ordered_styles() return ( ''.join(style.start_html for style in ordered_styles) + cgi.escape(self.text).encode('ascii', 'xmlcharrefreplace') + |