From 4c783ea3b7262ad49f70f010ece79cbc4f43e610 Mon Sep 17 00:00:00 2001
From: Richard Berger <richard.berger@temple.edu>
Date: Wed, 7 Sep 2016 01:31:56 -0400
Subject: [PATCH] Enforce l,ule or l,ole command order for RST

(cherry picked from commit 79e867c213ee022685e5bb8a5089a112099e4f06)
---
 doc/utils/converters/lammpsdoc/txt2html.py | 5 ++++-
 doc/utils/converters/lammpsdoc/txt2rst.py  | 8 ++++++++
 doc/utils/converters/tests/test_txt2rst.py | 8 ++++++++
 3 files changed, 20 insertions(+), 1 deletion(-)

diff --git a/doc/utils/converters/lammpsdoc/txt2html.py b/doc/utils/converters/lammpsdoc/txt2html.py
index ab132a380d..6e559723b4 100755
--- a/doc/utils/converters/lammpsdoc/txt2html.py
+++ b/doc/utils/converters/lammpsdoc/txt2html.py
@@ -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'
diff --git a/doc/utils/converters/lammpsdoc/txt2rst.py b/doc/utils/converters/lammpsdoc/txt2rst.py
index a4196e501b..1bc279c0f3 100755
--- a/doc/utils/converters/lammpsdoc/txt2rst.py
+++ b/doc/utils/converters/lammpsdoc/txt2rst.py
@@ -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 '
diff --git a/doc/utils/converters/tests/test_txt2rst.py b/doc/utils/converters/tests/test_txt2rst.py
index 1602fb61f9..2fa2bd699e 100644
--- a/doc/utils/converters/tests/test_txt2rst.py
+++ b/doc/utils/converters/tests/test_txt2rst.py
@@ -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"
-- 
GitLab