Skip to content
Snippets Groups Projects
Commit 838b6e0e authored by Lukáš Krupčík's avatar Lukáš Krupčík
Browse files

modified: scripts/titlemd.py

parent 5604406f
No related branches found
No related tags found
1 merge request!475modified: .gitlab-ci.yml
Pipeline #39792 failed
#!/usr/bin/env python3 #!/usr/bin/env python3
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# pylint: disable=C0301, R1710
""" titlemd """ """ titlemd """
import argparse import argparse
...@@ -12,7 +14,7 @@ except ImportError: ...@@ -12,7 +14,7 @@ except ImportError:
def arg_parse(): def arg_parse():
""" """
argument parser Argument parser
""" """
parser = argparse.ArgumentParser( parser = argparse.ArgumentParser(
description="Titlemd" description="Titlemd"
...@@ -47,16 +49,16 @@ def linestart(line, disabled, test, prev_line=None): ...@@ -47,16 +49,16 @@ def linestart(line, disabled, test, prev_line=None):
def testdata(arg): def testdata(arg):
""" test """ """ test """
# Spelling exceptions # Spelling exceptions
with open('.spelling') as fdata: with open('.spelling', encoding='utf-8') as fdata:
spelling = fdata.readlines() spelling = fdata.readlines()
def abbreviations(word, **kwargs): def abbreviations(word, **_):
""" abbreviations """ """ abbreviations """
if word+"\n" in spelling: if word + "\n" in spelling:
return word return word
# Open the file and read the lines as a list # Open the file and read the lines as a list
with open(arg.location) as fdata: with open(arg.location, encoding='utf-8') as fdata:
lines = fdata.readlines() lines = fdata.readlines()
# Loop through the list of lines and titlecase # Loop through the list of lines and titlecase
...@@ -68,64 +70,62 @@ def testdata(arg): ...@@ -68,64 +70,62 @@ def testdata(arg):
for line in lines: for line in lines:
disabled = linestart(line, disabled, arg.test, prev_line) disabled = linestart(line, disabled, arg.test, prev_line)
if line.startswith('#') and not disabled and not mkdocs_available(arg.location): if line.startswith('#') and not disabled and not mkdocs_available(arg.location):
if line != titlecase(line[:(line.find("]"))], title_line = titlecase(line[:line.find("]")], callback=abbreviations) + line[line.find("]"):]
callback=abbreviations)+line[(line.find("]")):]: if line != title_line:
if return_value == 0 and not echo_filename: if return_value == 0 and not echo_filename:
print(f"{arg.location}") print(f"{arg.location}")
echo_filename = True echo_filename = True
print(f"-{line}", end="") print(f"-{line}", end="")
print(f"+{titlecase(line[:(line.find(']'))], callback=abbreviations)}{line[(line.find(']')):]}", end="") print(f"+{title_line}", end="")
print() print()
return_value = 1 return_value = 1
if (line.startswith('---') or line.startswith('===')) and not disabled: if (line.startswith('---') or line.startswith('===')) and not disabled:
if prev_line != titlecase(prev_line[:(prev_line.find("]"))], title_prev_line = titlecase(prev_line[:prev_line.find("]")], callback=abbreviations) + prev_line[prev_line.find("]"):]
callback=abbreviations)+prev_line[(prev_line.find("]")):]: if prev_line != title_prev_line:
if return_value == 0 and not echo_filename: if return_value == 0 and not echo_filename:
print(f"{arg.location}") print(f"{arg.location}")
echo_filename = True echo_filename = True
print(f"-{prev_line}", end="") print(f"-{prev_line}", end="")
print(f"+{titlecase(prev_line[:(prev_line.find(']'))], callback=abbreviations)}{prev_line[(prev_line.find(']')):]}", end="") print(f"+{title_prev_line}", end="")
print() print()
return_value = 1 return_value = 1
if (mkdocs_available(arg.location) and not line.startswith('#') and not disabled): if (mkdocs_available(arg.location) and not line.startswith('#') and not disabled):
if line != titlecase(line[:(line.find(":"))], title_line = titlecase(line[:line.find(":")], callback=abbreviations) + line[line.find(":"):]
callback=abbreviations)+line[(line.find(":")):]: if line != title_line:
if return_value == 0 and not echo_filename: if return_value == 0 and not echo_filename:
print(f"{arg.location}") print(f"{arg.location}")
echo_filename = True echo_filename = True
print(f"-{line}", end="") print(f"-{line}", end="")
print(f"+{titlecase(line[:(line.find(':'))], callback=abbreviations)}{line[(line.find(':')):]}", end="") print(f"+{title_line}", end="")
print() print()
return_value = 1 return_value = 1
prev_line = line prev_line = line
return return_value return return_value
def writedata(arg): def writedata(arg):
""" writedata """ """ writedata """
# Spelling exceptions # Spelling exceptions
with open('.spelling') as fdata: with open('.spelling', encoding='utf-8') as fdata:
spelling = fdata.readlines() spelling = fdata.readlines()
def abbreviations(word, **kwargs): def abbreviations(word, **_):
""" abbreviations """ """ abbreviations """
if word+"\n" in spelling: if word + "\n" in spelling:
return word return word
# Open the file and read the lines as a list # Open the file and read the lines as a list
with open(arg.location) as fdata: with open(arg.location, encoding='utf-8') as fdata:
lines = fdata.readlines() lines = fdata.readlines()
with open(arg.location, 'w') as fdata: with open(arg.location, 'w', encoding='utf-8') as fdata:
prev_line = lines[0] if lines else "" prev_line = lines[0] if lines else ""
disabled = False disabled = False
for line in lines: for line in lines:
disabled = linestart(line, disabled, arg.test) disabled = linestart(line, disabled, arg.test)
if line.startswith('#') and not disabled: if line.startswith('#') and not disabled:
line = titlecase(line[:(line.find("]"))], line = titlecase(line[:line.find("]")], callback=abbreviations) + line[line.find("]"):]
callback=abbreviations)+line[(line.find("]")):]
if (line.startswith('---') or line.startswith('===')) and not disabled: if (line.startswith('---') or line.startswith('===')) and not disabled:
prev_line = titlecase(prev_line[:(prev_line.find("]"))], prev_line = titlecase(prev_line[:prev_line.find("]")], callback=abbreviations) + prev_line[prev_line.find("]"):]
callback=abbreviations)+prev_line[(prev_line.find("]")):]
fdata.write(prev_line) fdata.write(prev_line)
prev_line = line prev_line = line
fdata.write(prev_line) fdata.write(prev_line)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment