2023-05-03 14:59:06 +02:00
|
|
|
"""
|
|
|
|
|
Program for converting Latex files into html files
|
|
|
|
|
"""
|
|
|
|
|
|
2023-05-03 20:29:08 +02:00
|
|
|
from code.latex_classes.latex_classes import return_latex_classes
|
|
|
|
|
from code.error_messages.error_arrays import return_error_arrays
|
|
|
|
|
|
2023-05-22 23:47:47 +02:00
|
|
|
|
2023-05-03 21:41:49 +02:00
|
|
|
def command_name_check(latex_string, command_name):
|
|
|
|
|
error_arrays = return_error_arrays()
|
2023-05-22 23:47:47 +02:00
|
|
|
if latex_string[1: (len(command_name) + 1)] != command_name:
|
2023-05-03 21:41:49 +02:00
|
|
|
print(latex_string + error_arrays[4])
|
|
|
|
|
return "Error!"
|
|
|
|
|
return
|
|
|
|
|
|
2023-05-22 23:47:47 +02:00
|
|
|
|
2023-05-03 20:55:46 +02:00
|
|
|
def generic_checks(latex_string):
|
|
|
|
|
error_arrays = return_error_arrays()
|
|
|
|
|
if latex_string == "":
|
|
|
|
|
print(latex_string + error_arrays[0])
|
|
|
|
|
return "Error!"
|
|
|
|
|
if "}" not in latex_string:
|
|
|
|
|
print(latex_string + error_arrays[1])
|
|
|
|
|
return "Error!"
|
2023-05-10 18:16:28 +02:00
|
|
|
return
|
|
|
|
|
|
2023-05-22 23:47:47 +02:00
|
|
|
|
2023-05-10 18:16:28 +02:00
|
|
|
def generic_checks_command(latex_string):
|
|
|
|
|
error_arrays = return_error_arrays()
|
|
|
|
|
if generic_checks(latex_string) == "Error!":
|
|
|
|
|
return "Error!"
|
2023-05-03 20:55:46 +02:00
|
|
|
if latex_string[0] != "\\":
|
|
|
|
|
print(latex_string + error_arrays[6])
|
|
|
|
|
return "Error!"
|
2023-05-03 21:41:49 +02:00
|
|
|
return
|
2023-05-03 20:55:46 +02:00
|
|
|
|
2023-05-03 14:59:06 +02:00
|
|
|
|
|
|
|
|
def document_class(latex_string):
|
2023-05-03 20:29:08 +02:00
|
|
|
r"""
|
2023-05-03 14:59:06 +02:00
|
|
|
Converts LaTeX documentclass method to html
|
2023-05-03 20:29:08 +02:00
|
|
|
\documentclass{article}
|
2023-05-03 14:59:06 +02:00
|
|
|
"""
|
2023-05-03 20:29:08 +02:00
|
|
|
error_arrays = return_error_arrays()
|
2023-05-10 18:16:28 +02:00
|
|
|
if generic_checks_command(latex_string) == "Error!":
|
2023-05-03 20:29:08 +02:00
|
|
|
return "Error!"
|
|
|
|
|
if latex_string[len("\\documentclass{") - 1] != "{":
|
2023-05-03 20:55:46 +02:00
|
|
|
print(latex_string + error_arrays[3])
|
2023-05-03 20:29:08 +02:00
|
|
|
return "Error!"
|
2023-05-03 21:41:49 +02:00
|
|
|
if command_name_check(latex_string, "documentclass") == "Error!":
|
2023-05-03 20:29:08 +02:00
|
|
|
return "Error!"
|
2023-05-22 23:47:47 +02:00
|
|
|
document_type = latex_string[len(
|
|
|
|
|
"\\documentclass{"): (len(latex_string) - 1)]
|
2023-05-03 20:55:46 +02:00
|
|
|
latex_classes = return_latex_classes()
|
2023-05-03 20:29:08 +02:00
|
|
|
if document_type not in latex_classes:
|
|
|
|
|
return f"Error! class {document_type} is not known!"
|
2023-05-03 20:55:46 +02:00
|
|
|
return "<!DOCTYPE html>"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def begin_document(latex_string):
|
|
|
|
|
r"""
|
|
|
|
|
Converts LaTeX begin document method to html
|
|
|
|
|
\begin{document}
|
|
|
|
|
"""
|
2023-05-10 18:16:28 +02:00
|
|
|
if generic_checks_command(latex_string) == "Error!":
|
2023-05-03 20:55:46 +02:00
|
|
|
return "Error!"
|
2023-05-03 21:41:49 +02:00
|
|
|
if command_name_check(latex_string, "begin") == "Error!":
|
2023-05-03 20:55:46 +02:00
|
|
|
return "Error!"
|
|
|
|
|
return "<html>"
|
|
|
|
|
|
2023-05-22 23:47:47 +02:00
|
|
|
|
2023-05-03 21:41:49 +02:00
|
|
|
def begin_tabular(latex_string):
|
|
|
|
|
r"""
|
|
|
|
|
Checks if LaTeX begin tabular method is correct
|
|
|
|
|
"""
|
2023-05-10 18:16:28 +02:00
|
|
|
if generic_checks_command(latex_string) == "Error!":
|
2023-05-03 21:41:49 +02:00
|
|
|
return "Error!"
|
|
|
|
|
if command_name_check(latex_string, "begin") == "Error!":
|
|
|
|
|
return "Error!"
|
|
|
|
|
return "<table>"
|
|
|
|
|
|
2023-05-22 23:47:47 +02:00
|
|
|
|
2023-05-03 21:41:49 +02:00
|
|
|
def tabular_parameters(latex_string):
|
|
|
|
|
r"""
|
|
|
|
|
Checks if LaTeX tabular environment has any parameters
|
|
|
|
|
"""
|
|
|
|
|
if latex_string == "":
|
|
|
|
|
return ""
|
|
|
|
|
if latex_string == "[]":
|
|
|
|
|
return ""
|
|
|
|
|
return "Error!"
|
|
|
|
|
|
2023-05-22 23:47:47 +02:00
|
|
|
|
2023-05-30 22:43:42 +02:00
|
|
|
def only_pipes_and_space(latex_string):
|
|
|
|
|
"""Checks if latex string only contains | or " ", if yes returns True, if no returns false"""
|
|
|
|
|
return all(char == '|' or char == ' ' or char == '{' or char == '}' for char in latex_string)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def main_tabular_parameters_loop(latex_string, simple_parameters_dictionary):
|
|
|
|
|
return_array = []
|
|
|
|
|
i = 0
|
|
|
|
|
latex_string_length = len(latex_string)
|
|
|
|
|
while i < latex_string_length:
|
|
|
|
|
character = latex_string[i]
|
|
|
|
|
if character in ['l', 'c', 'r', '|']:
|
|
|
|
|
print("entered")
|
|
|
|
|
return_array.append(
|
|
|
|
|
simple_parameters_dictionary.get(latex_string[i]))
|
|
|
|
|
i += 1
|
|
|
|
|
continue
|
|
|
|
|
if character in ['p', 'm', 'b']:
|
|
|
|
|
closing_bracket = latex_string.find('}', i + 1)
|
|
|
|
|
columns_string = latex_string[i:closing_bracket + 1]
|
|
|
|
|
print(columns_string)
|
|
|
|
|
result = tabular_columns_parameters(columns_string)
|
|
|
|
|
if result == "Error!":
|
|
|
|
|
return result
|
|
|
|
|
return_array.append(result)
|
|
|
|
|
i = closing_bracket
|
|
|
|
|
continue
|
|
|
|
|
i += 1
|
|
|
|
|
print(return_array)
|
|
|
|
|
return return_array
|
|
|
|
|
|
|
|
|
|
|
2023-05-22 23:47:47 +02:00
|
|
|
def tabular_required_parameters(latex_string):
|
2023-05-10 18:16:28 +02:00
|
|
|
if generic_checks(latex_string) == "Error!":
|
|
|
|
|
return "Error!"
|
2023-05-22 23:47:47 +02:00
|
|
|
if latex_string == "{}":
|
|
|
|
|
print("tabular_required_parameters, required table parameters are empty!:", latex_string)
|
|
|
|
|
return "Error!"
|
|
|
|
|
simple_parameters_dictionary = {
|
|
|
|
|
"l": "align='left'",
|
|
|
|
|
"c": "align='center'",
|
|
|
|
|
"r": "align='right'",
|
2023-05-31 02:14:25 +02:00
|
|
|
"|": "style='border-left: 1px solid black'"
|
2023-05-22 23:47:47 +02:00
|
|
|
}
|
2023-05-30 22:43:42 +02:00
|
|
|
if only_pipes_and_space(latex_string):
|
|
|
|
|
print("tabular_required_parameters, required table parameters are only pipes and spaces!:", latex_string)
|
|
|
|
|
return "Error!"
|
|
|
|
|
return main_tabular_parameters_loop(latex_string, simple_parameters_dictionary)
|
2023-05-10 18:16:28 +02:00
|
|
|
|
2023-05-22 23:47:47 +02:00
|
|
|
|
2023-05-10 18:16:28 +02:00
|
|
|
def length_conversions(latex_length):
|
|
|
|
|
length_dictionary = {
|
|
|
|
|
"pt": [1.3, "px"],
|
|
|
|
|
|
|
|
|
|
"mm": [1, "mm"],
|
|
|
|
|
|
|
|
|
|
"cm": [1, "cm"],
|
|
|
|
|
|
|
|
|
|
"in": [1, "in"],
|
|
|
|
|
|
|
|
|
|
"ex": [1, "ex"],
|
|
|
|
|
|
|
|
|
|
"em": [1, "em"],
|
|
|
|
|
}
|
|
|
|
|
return length_dictionary.get(latex_length, "Error!")
|
|
|
|
|
|
2023-05-22 23:47:47 +02:00
|
|
|
|
|
|
|
|
def tabular_columns_parameters(latex_string):
|
|
|
|
|
if generic_checks(latex_string) == "Error!":
|
|
|
|
|
return "Error!"
|
|
|
|
|
parameter_dictionary = {
|
|
|
|
|
"p": "vertical-align: top;",
|
|
|
|
|
"m": "vertical-align: middle;",
|
|
|
|
|
"b": "vertical-align: bottom;"
|
|
|
|
|
}
|
|
|
|
|
vertical_align_type = parameter_dictionary.get(latex_string[0], "Error!")
|
|
|
|
|
if vertical_align_type == "Error!":
|
|
|
|
|
print("tabular_columns_parameters, unknown parameter: ",
|
|
|
|
|
latex_string[0])
|
|
|
|
|
return "Error!"
|
|
|
|
|
if latex_string[1] != '{':
|
|
|
|
|
print(
|
|
|
|
|
"tabular_columns_parameters, parameter length does not start with {", latex_string)
|
|
|
|
|
return "Error!"
|
|
|
|
|
length_parameter_with_bracket = latex_string.partition("{")[2]
|
|
|
|
|
length_parameter = length_parameter_with_bracket.partition("}")[0]
|
|
|
|
|
length_value = ""
|
|
|
|
|
i = 0
|
|
|
|
|
for character in length_parameter:
|
|
|
|
|
if character.isalpha():
|
|
|
|
|
break
|
|
|
|
|
length_value += character
|
|
|
|
|
i += 1
|
|
|
|
|
length_unit = length_parameter[i:]
|
|
|
|
|
conversed_unit = length_conversions(length_unit)
|
|
|
|
|
if conversed_unit == "Error!":
|
|
|
|
|
print("tabular_columns_parameters, Unit could not be conversed!", latex_string)
|
|
|
|
|
return "Error!"
|
|
|
|
|
print(length_value, conversed_unit, conversed_unit[0])
|
|
|
|
|
final_length = round(float(length_value) * conversed_unit[0], 2)
|
2023-05-31 02:14:25 +02:00
|
|
|
return_string = "style='" + vertical_align_type + \
|
|
|
|
|
" width: " + str(final_length) + conversed_unit[1] + ";'"
|
2023-05-22 23:47:47 +02:00
|
|
|
return return_string
|
|
|
|
|
|
|
|
|
|
|
2023-05-31 02:14:25 +02:00
|
|
|
def split_rows(latex_string):
|
|
|
|
|
double_backslash = "\\"
|
|
|
|
|
rows = latex_string.split(double_backslash)
|
|
|
|
|
print(rows)
|
|
|
|
|
return rows
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def split_columns(table_row, column_count):
|
|
|
|
|
columns = table_row.split("&")
|
|
|
|
|
if len(columns) != column_count and columns != ['']:
|
|
|
|
|
print(
|
|
|
|
|
f"split_columns, table_row: {table_row} has different amount of columns than expected: {column_count}")
|
|
|
|
|
return "Error!"
|
|
|
|
|
return columns
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def translate_column(latex_column):
|
|
|
|
|
hline_string_literal = "\hline"
|
|
|
|
|
replaced_hline = latex_column.replace(hline_string_literal, "<hr>")
|
|
|
|
|
replaced_newline = replaced_hline.replace('\newline', "<br>")
|
|
|
|
|
print(latex_column, replaced_newline,
|
|
|
|
|
latex_column.find(hline_string_literal), hline_string_literal)
|
|
|
|
|
return replaced_newline
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def translate_inside_to_html(latex_table_inside, column_style):
|
|
|
|
|
return_string = "<html> <table>"
|
|
|
|
|
column_amount = 0
|
|
|
|
|
line_string = "style='border-left: 1px solid black'"
|
|
|
|
|
for style in column_style:
|
|
|
|
|
print(style)
|
|
|
|
|
if style != line_string:
|
|
|
|
|
column_amount += 1
|
|
|
|
|
rows = split_rows(latex_table_inside)
|
|
|
|
|
for row in rows:
|
|
|
|
|
return_string += "<tr>"
|
|
|
|
|
|
|
|
|
|
columns = split_columns(row, column_amount)
|
|
|
|
|
column_number = 0
|
|
|
|
|
for column in columns:
|
|
|
|
|
return_string += "<td "
|
|
|
|
|
current_style = column_style[column_number]
|
|
|
|
|
while current_style == line_string:
|
|
|
|
|
return_string += line_string
|
|
|
|
|
column_number += 1
|
|
|
|
|
current_style = column_style[column_number]
|
|
|
|
|
|
|
|
|
|
return_string += column_style[column_number] + ">"
|
|
|
|
|
return_string += translate_column(column)
|
|
|
|
|
column_number += 1
|
|
|
|
|
return_string += "</td>"
|
|
|
|
|
|
|
|
|
|
return_string += "</tr>"
|
|
|
|
|
return_string += " </table> </html>"
|
|
|
|
|
print(return_string)
|
|
|
|
|
return return_string
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def read_file(tex_filename: string):
|
|
|
|
|
tex_file = open(tex_filename, "r")
|
|
|
|
|
data = tex_file.read()
|
|
|
|
|
tex_file.close()
|
|
|
|
|
return data
|
2023-05-03 20:29:08 +02:00
|
|
|
|
2023-05-22 23:47:47 +02:00
|
|
|
|
2023-05-03 20:29:08 +02:00
|
|
|
if __name__ == "__main__":
|
2023-05-31 02:14:25 +02:00
|
|
|
tex_filename = "texfile.tex"
|
|
|
|
|
data = read_file(tex_filename)
|
|
|
|
|
document_class_index = data.find("\documentclass")
|
|
|
|
|
if document_class_index == -1:
|
|
|
|
|
print("Main function error! documentclass not found")
|
|
|
|
|
return "Error!"
|
2023-05-03 20:29:08 +02:00
|
|
|
document_class("")
|