Commit 4c783ea3 authored by Richard Berger's avatar Richard Berger Committed by Axel Kohlmeyer
Browse files

Enforce l,ule or l,ole command order for RST

(cherry picked from commit 79e867c213ee022685e5bb8a5089a112099e4f06)
parent 9e8256ae
Loading
Loading
Loading
Loading
+4 −1
Original line number Diff line number Diff line
@@ -518,6 +518,9 @@ class TxtParser(object):
    def last_word(self, text):
        return text.split()[-1]

    def order_commands(self, commands):
        return list(reversed(commands))

    def do_formatting(self, paragraph):
        last_word = self.last_word(paragraph)
        format_str = paragraph[paragraph.rfind(last_word):]
@@ -529,7 +532,7 @@ class TxtParser(object):

        commands = [x[0] for x in command_pattern.findall(commands)]

        for command in reversed(commands):
        for command in self.order_commands(commands):
            paragraph = self.format.convert(command, paragraph, commands)

        return paragraph + '\n'
+8 −0
Original line number Diff line number Diff line
@@ -347,6 +347,14 @@ class Txt2Rst(TxtParser):
    def is_raw_textblock_end(self, line):
        return line.startswith('END_RST -->')

    def order_commands(self, commands):
        if 'ule' in commands and 'l' in commands and commands.index('ule') >  commands.index('l'):
            return commands
        elif 'ole' in commands and 'l' in commands and commands.index('ole') > commands.index('l'):
            return commands
        return super().order_commands(commands)


class Txt2RstConverter(TxtConverter):
    def get_argument_parser(self):
        parser = argparse.ArgumentParser(description='converts a text file with simple formatting & markup into '
+8 −0
Original line number Diff line number Diff line
@@ -236,6 +236,14 @@ class TestListFormatting(unittest.TestCase):
                         "* two\n"
                         "* three\n\n", s)

    def test_elementwise_unordered_list_reverse(self):
        s = self.txt2rst.convert("one :ulb,l\n"
                                 "two :l\n"
                                 "three :l,ule\n")
        self.assertEqual("* one\n"
                         "* two\n"
                         "* three\n\n", s)

    def test_multi_line_unordered_list_elements(self):
        s = self.txt2rst.convert("one :ulb,l\n"
                                 "two\n"