From a2a1c09ffb890b5a4717a54ccefb6c00fb24ede4 Mon Sep 17 00:00:00 2001 From: Polawat Phetra Date: Wed, 26 Nov 2014 08:41:43 +0700 Subject: [PATCH 1/3] fix case: Control "f0" in RTF file from Mac OS X did not occur in subgroup. --- pyth/plugins/rtf15/reader.py | 23 ++++++++++++++--------- tests/test_readosxrtf.py | 24 ++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 9 deletions(-) create mode 100644 tests/test_readosxrtf.py diff --git a/pyth/plugins/rtf15/reader.py b/pyth/plugins/rtf15/reader.py index 581d55a..02928f8 100644 --- a/pyth/plugins/rtf15/reader.py +++ b/pyth/plugins/rtf15/reader.py @@ -485,15 +485,20 @@ def handle_f(self, fontNum): if 'FONT_TABLE' in (self.parent.specialMeaning, self.specialMeaning): self.fontNum = int(fontNum) self._setFontCharset() - elif self.charsetTable is not None: - try: - self.charset = self.charsetTable[int(fontNum)] - except KeyError: - # fontNum not found in charsetTable, ignore if requested - if self.reader.errors == 'ignore': - pass - else: - raise + else: + charsetTable = self.charsetTable + if not charsetTable: + charsetTable = self.reader.charsetTable + + if charsetTable is not None: + try: + self.charset = charsetTable[int(fontNum)] + except KeyError: + # fontNum not found in charsetTable, ignore if requested + if self.reader.errors == 'ignore': + pass + else: + raise def handle_fcharset(self, charsetNum): if 'FONT_TABLE' in (self.parent.specialMeaning, self.specialMeaning): diff --git a/tests/test_readosxrtf.py b/tests/test_readosxrtf.py new file mode 100644 index 0000000..4698ca5 --- /dev/null +++ b/tests/test_readosxrtf.py @@ -0,0 +1,24 @@ + +# -*- coding: UTF-8 -*- +""" +Unit tests of the rtf15 reader. +""" + +import unittest + +from pyth.plugins.rtf15.reader import Rtf15Reader +from pyth.plugins.plaintext.writer import PlaintextWriter +from StringIO import StringIO + +class TestReadOSXRtf(unittest.TestCase): + + def test_read(self): + rtf = StringIO("""{\\rtf1\\ansi\\ansicpg1252\\cocoartf1343\\cocoasubrtf160\\cocoascreenfonts1{\\fonttbl\\f0\\fnil\\fcharset222 Thonburi;} +{\\colortbl;\\red255\\green255\\blue255;} +\\pard\\tx560\\tx1120\\tx1680\\tx2240\\tx2800\\tx3360\\tx3920\\tx4480\\tx5040\\tx5600\\tx6160\\tx6720\\pardirnatural\\qc + +\\f0\\fs24 \\cf0 \\'b9\\'e9\\'d3\\'b5\\'a1""") + doc = Rtf15Reader.read(rtf) + text = PlaintextWriter.write(doc).read() + print text + self.assertEquals(u"น้ำตก", text.decode('utf8')) \ No newline at end of file From 3399e9064aae25e0a40575001ba96150a1a82a25 Mon Sep 17 00:00:00 2001 From: Polawat Phetra Date: Fri, 28 Nov 2014 16:28:28 +0700 Subject: [PATCH 2/3] remove codecs.register() that interfere django-rest-framework. --- pyth/encodings/symbol.py | 2 +- tests/test_readosxrtf.py | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/pyth/encodings/symbol.py b/pyth/encodings/symbol.py index 1289b85..9224fc7 100644 --- a/pyth/encodings/symbol.py +++ b/pyth/encodings/symbol.py @@ -100,4 +100,4 @@ def search(name): return info return None -codecs.register(search) +#codecs.register(search) diff --git a/tests/test_readosxrtf.py b/tests/test_readosxrtf.py index 4698ca5..28595b9 100644 --- a/tests/test_readosxrtf.py +++ b/tests/test_readosxrtf.py @@ -18,6 +18,18 @@ def test_read(self): \\pard\\tx560\\tx1120\\tx1680\\tx2240\\tx2800\\tx3360\\tx3920\\tx4480\\tx5040\\tx5600\\tx6160\\tx6720\\pardirnatural\\qc \\f0\\fs24 \\cf0 \\'b9\\'e9\\'d3\\'b5\\'a1""") + doc = Rtf15Reader.read(rtf) + text = PlaintextWriter.write(doc).read() + print text + self.assertEquals(u"น้ำตก", text.decode('utf8')) + + + def test_read2(self): + rtf = StringIO("""{\\rtf1\\ansi\\ansicpg1252\\cocoartf1343\\cocoasubrtf160\\cocoascreenfonts1{\\fonttbl\\f0\\fnil\\fcharset222 Thonburi;} +{\\colortbl;\\red255\\green255\\blue255;} +\\pard\\tx560\\tx1120\\tx1680\\tx2240\\tx2800\\tx3360\\tx3920\\tx4480\\tx5040\\tx5600\\tx6160\\tx6720\\pardirnatural\\qc + +{\\f0\\fs24 \\cf0 \\'b9\\'e9\\'d3\\'b5\\'a1}""") doc = Rtf15Reader.read(rtf) text = PlaintextWriter.write(doc).read() print text From 02f7f92b4bf2cc2846ce1b90884f9105cb7a105c Mon Sep 17 00:00:00 2001 From: pphetra Date: Mon, 26 Jun 2017 21:14:36 +0700 Subject: [PATCH 3/3] update minor version --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 8b2433a..6ab0bc7 100755 --- a/setup.py +++ b/setup.py @@ -1,7 +1,7 @@ from setuptools import setup, find_packages setup(name="pyth", - version="0.6.0", + version="0.6.1", packages = find_packages(), zip_safe = False,