forked from lijiext/lammps
Raise exception and output error if ulb,ule and olb,ole are unbalanced
This commit is contained in:
parent
1dc19eceb2
commit
c61d5a1a29
|
@ -25,6 +25,7 @@ import re
|
||||||
import sys
|
import sys
|
||||||
import argparse
|
import argparse
|
||||||
|
|
||||||
|
|
||||||
class Markup(object):
|
class Markup(object):
|
||||||
BOLD_START = "["
|
BOLD_START = "["
|
||||||
BOLD_END = "]"
|
BOLD_END = "]"
|
||||||
|
@ -77,6 +78,7 @@ class Markup(object):
|
||||||
text = text.replace('\"%s\"_%s' % (name, link), href, 1)
|
text = text.replace('\"%s\"_%s' % (name, link), href, 1)
|
||||||
return text
|
return text
|
||||||
|
|
||||||
|
|
||||||
class HTMLMarkup(Markup):
|
class HTMLMarkup(Markup):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
|
@ -101,6 +103,7 @@ class HTMLMarkup(Markup):
|
||||||
|
|
||||||
return "<A HREF = \"" + href + "\">" + content + "</A>"
|
return "<A HREF = \"" + href + "\">" + content + "</A>"
|
||||||
|
|
||||||
|
|
||||||
class Formatting(object):
|
class Formatting(object):
|
||||||
UNORDERED_LIST_MODE = "unordered-list"
|
UNORDERED_LIST_MODE = "unordered-list"
|
||||||
ORDERED_LIST_MODE = "ordered-list"
|
ORDERED_LIST_MODE = "ordered-list"
|
||||||
|
@ -435,6 +438,7 @@ class Formatting(object):
|
||||||
|
|
||||||
return rows
|
return rows
|
||||||
|
|
||||||
|
|
||||||
class HTMLFormatting(Formatting):
|
class HTMLFormatting(Formatting):
|
||||||
def __init__(self, markup):
|
def __init__(self, markup):
|
||||||
super().__init__(markup)
|
super().__init__(markup)
|
||||||
|
@ -448,6 +452,7 @@ class HTMLFormatting(Formatting):
|
||||||
def raw_html(self, content):
|
def raw_html(self, content):
|
||||||
return content
|
return content
|
||||||
|
|
||||||
|
|
||||||
class TxtParser(object):
|
class TxtParser(object):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.markup = HTMLMarkup()
|
self.markup = HTMLMarkup()
|
||||||
|
@ -630,6 +635,7 @@ class TxtParser(object):
|
||||||
|
|
||||||
i += 1
|
i += 1
|
||||||
|
|
||||||
|
|
||||||
class Txt2Html(TxtParser):
|
class Txt2Html(TxtParser):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
|
@ -641,6 +647,7 @@ class Txt2Html(TxtParser):
|
||||||
line.startswith(".. END_HTML_ONLY") or \
|
line.startswith(".. END_HTML_ONLY") or \
|
||||||
super().is_paragraph_separator(line)
|
super().is_paragraph_separator(line)
|
||||||
|
|
||||||
|
|
||||||
class TxtConverter:
|
class TxtConverter:
|
||||||
def get_argument_parser(self):
|
def get_argument_parser(self):
|
||||||
return None
|
return None
|
||||||
|
@ -665,7 +672,15 @@ class TxtConverter:
|
||||||
print("Converting", filename, "...", file=err)
|
print("Converting", filename, "...", file=err)
|
||||||
content = f.read()
|
content = f.read()
|
||||||
converter = self.create_converter(parsed_args)
|
converter = self.create_converter(parsed_args)
|
||||||
result = converter.convert(content)
|
|
||||||
|
try:
|
||||||
|
result = converter.convert(content)
|
||||||
|
except Exception as e:
|
||||||
|
msg = "###########################################################################\n" \
|
||||||
|
" ERROR: " + e.args[0] + "\n" \
|
||||||
|
"###########################################################################\n"
|
||||||
|
print(msg, file=err)
|
||||||
|
result = msg
|
||||||
|
|
||||||
if write_to_files:
|
if write_to_files:
|
||||||
output_filename = self.get_output_filename(filename)
|
output_filename = self.get_output_filename(filename)
|
||||||
|
@ -674,6 +689,7 @@ class TxtConverter:
|
||||||
else:
|
else:
|
||||||
print(result, end='', file=out)
|
print(result, end='', file=out)
|
||||||
|
|
||||||
|
|
||||||
class Txt2HtmlConverter(TxtConverter):
|
class Txt2HtmlConverter(TxtConverter):
|
||||||
def get_argument_parser(self):
|
def get_argument_parser(self):
|
||||||
parser = argparse.ArgumentParser(description='converts a text file with simple formatting & markup into HTML.\n'
|
parser = argparse.ArgumentParser(description='converts a text file with simple formatting & markup into HTML.\n'
|
||||||
|
|
|
@ -24,6 +24,7 @@ import argparse
|
||||||
from lammpsdoc import lammps_filters
|
from lammpsdoc import lammps_filters
|
||||||
from lammpsdoc.txt2html import Markup, Formatting, TxtParser, TxtConverter
|
from lammpsdoc.txt2html import Markup, Formatting, TxtParser, TxtConverter
|
||||||
|
|
||||||
|
|
||||||
class RSTMarkup(Markup):
|
class RSTMarkup(Markup):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
|
@ -340,6 +341,7 @@ class RSTFormatting(Formatting):
|
||||||
|
|
||||||
return text + post
|
return text + post
|
||||||
|
|
||||||
|
|
||||||
class Txt2Rst(TxtParser):
|
class Txt2Rst(TxtParser):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
|
@ -373,6 +375,11 @@ class Txt2Rst(TxtParser):
|
||||||
return commands
|
return commands
|
||||||
return super().order_commands(commands)
|
return super().order_commands(commands)
|
||||||
|
|
||||||
|
def transform_paragraphs(self, content):
|
||||||
|
if self.format.indent_level > 0:
|
||||||
|
raise Exception("unbalanced number of ulb,ule or olb,ole pairs!")
|
||||||
|
return super().transform_paragraphs(content)
|
||||||
|
|
||||||
|
|
||||||
class Txt2RstConverter(TxtConverter):
|
class Txt2RstConverter(TxtConverter):
|
||||||
def get_argument_parser(self):
|
def get_argument_parser(self):
|
||||||
|
@ -389,6 +396,7 @@ class Txt2RstConverter(TxtConverter):
|
||||||
filename, ext = os.path.splitext(path)
|
filename, ext = os.path.splitext(path)
|
||||||
return filename + ".rst"
|
return filename + ".rst"
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
app = Txt2RstConverter()
|
app = Txt2RstConverter()
|
||||||
app.run()
|
app.run()
|
||||||
|
|
Loading…
Reference in New Issue