Skip to content

Commit 91f0dde

Browse files
author
aa-turner
committed
Adopt RomanNumeral
git-svn-id: svn://svn.code.sf.net/p/docutils/code/trunk@9846 929543f6-e4f2-0310-98a6-ba3bd3dd1d04
1 parent 5037058 commit 91f0dde

5 files changed

Lines changed: 22 additions & 27 deletions

File tree

docutils/docutils/parsers/rst/states.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,12 @@
114114
import docutils.parsers.rst
115115
from docutils.parsers.rst import directives, languages, tableparser, roles
116116
from docutils.utils import escape2null, column_width
117-
from docutils.utils import punctuation_chars, roman, urischemes
117+
from docutils.utils import punctuation_chars, urischemes
118118
from docutils.utils import split_escaped_whitespace
119+
from docutils.utils._roman_numerals import (
120+
InvalidRomanNumeralError,
121+
RomanNumeral,
122+
)
119123

120124

121125
class MarkupError(DataError): pass
@@ -1067,10 +1071,6 @@ def _upperalpha_to_int(s, _zero=(ord('A')-1)):
10671071
return ord(s) - _zero
10681072

10691073

1070-
def _lowerroman_to_int(s):
1071-
return roman.fromRoman(s.upper())
1072-
1073-
10741074
class Body(RSTState):
10751075

10761076
"""
@@ -1098,8 +1098,8 @@ class Body(RSTState):
10981098
enum.converters = {'arabic': int,
10991099
'loweralpha': _loweralpha_to_int,
11001100
'upperalpha': _upperalpha_to_int,
1101-
'lowerroman': _lowerroman_to_int,
1102-
'upperroman': roman.fromRoman}
1101+
'lowerroman': RomanNumeral.from_string,
1102+
'upperroman': RomanNumeral.from_string}
11031103

11041104
enum.sequenceregexps = {}
11051105
for sequence in enum.sequences:
@@ -1382,8 +1382,8 @@ def parse_enumerator(self, match, expected_sequence=None):
13821382
ordinal = 1
13831383
else:
13841384
try:
1385-
ordinal = self.enum.converters[sequence](text)
1386-
except roman.InvalidRomanNumeralError:
1385+
ordinal = int(self.enum.converters[sequence](text))
1386+
except InvalidRomanNumeralError:
13871387
ordinal = None
13881388
return format, sequence, text, ordinal
13891389

@@ -1433,8 +1433,8 @@ def make_enumerator(self, ordinal, sequence, format):
14331433
enumerator = chr(ordinal + ord('a') - 1)
14341434
elif sequence.endswith('roman'):
14351435
try:
1436-
enumerator = roman.toRoman(ordinal)
1437-
except roman.RomanError:
1436+
enumerator = RomanNumeral(ordinal).to_uppercase()
1437+
except TypeError:
14381438
return None
14391439
else: # shouldn't happen
14401440
raise ParserError('unknown enumerator sequence: "%s"'

docutils/docutils/utils/_roman_numerals.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# $Id$
2-
# Author: Adam Turner.
2+
# Author: Adam Turner.
33
# Copyright: This module is placed in the public domain
44
# or under the `Zero Clause BSD licence`_,
55
# whichever is more permissive.

docutils/docutils/writers/latex2e/__init__.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,10 @@
1717
import string
1818
from urllib.request import url2pathname
1919
import warnings
20-
try:
21-
import roman
22-
except ImportError:
23-
import docutils.utils.roman as roman
2420

2521
from docutils import frontend, nodes, languages, writers, utils
2622
from docutils.transforms import writer_aux
23+
from docutils.utils._roman_numerals import RomanNumeral
2724
from docutils.utils.math import pick_math_environment, unichar2tex
2825

2926
LATEX_WRITER_DIR = Path(__file__).parent
@@ -2196,7 +2193,7 @@ def visit_enumerated_list(self, node) -> None:
21962193
suffix = node.get('suffix', '.')
21972194

21982195
enum_level = len(self._enumeration_counters)+1
2199-
counter_name = 'enum' + roman.toRoman(enum_level).lower()
2196+
counter_name = 'enum' + RomanNumeral(enum_level).to_lowercase()
22002197
label = r'%s\%s{%s}%s' % (prefix, enumtype, counter_name, suffix)
22012198
self._enumeration_counters.append(label)
22022199

@@ -3139,13 +3136,13 @@ def visit_title(self, node) -> None:
31393136
# section level not supported by LaTeX
31403137
if self.settings.legacy_class_functions:
31413138
self.fallbacks['title'] = PreambleCmds.title_legacy
3142-
section_name += '[section%s]' % roman.toRoman(level)
3139+
section_name += '[section%s]' % RomanNumeral(level)
31433140
else:
31443141
if not self.fallback_stylesheet:
31453142
self.fallbacks['title'] = PreambleCmds.title
31463143
self.fallbacks['DUclass'] = PreambleCmds.duclass
31473144
self.out.append('\\begin{DUclass}{section%s}\n'
3148-
% roman.toRoman(level))
3145+
% RomanNumeral(level))
31493146

31503147
# System messages heading in red:
31513148
if 'system-messages' in node.parent['classes']:

docutils/docutils/writers/manpage.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,7 @@
4747

4848
import docutils
4949
from docutils import frontend, nodes, writers, languages
50-
try:
51-
import roman
52-
except ImportError:
53-
from docutils.utils import roman
50+
from docutils.utils._roman_numerals import RomanNumeral
5451

5552
FIELD_LIST_INDENT = 7
5653
DEFINITION_LIST_INDENT = 7
@@ -408,10 +405,11 @@ def __next__(self):
408405
if self._style in ('loweralpha', 'upperalpha'):
409406
return "%c." % self._cnt
410407
if self._style.endswith('roman'):
411-
res = roman.toRoman(self._cnt) + '.'
408+
res = RomanNumeral(self._cnt)
412409
if self._style.startswith('upper'):
413-
return res.upper()
414-
return res.lower()
410+
return res.to_uppercase() + '.'
411+
else:
412+
return res.to_lowercase() + '.'
415413
# else 'arabic', ...
416414
return "%d." % self._cnt
417415

docutils/test/test_utils/test__roman_numerals.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#! /usr/bin/env python3
22

33
# $Id$
4-
# Author: Adam Turner.
4+
# Author: Adam Turner.
55
# Copyright: This module is placed in the public domain
66
# or under the `Zero Clause BSD licence`_,
77
# whichever is more permissive.

0 commit comments

Comments
 (0)