Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge branch 'issue-10'
  • Loading branch information
ijacobs-cpa committed Sep 28, 2023
2 parents 23098af + 0b994c7 commit c1e4bff
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 11 deletions.
6 changes: 5 additions & 1 deletion README.md
Expand Up @@ -46,6 +46,7 @@ or
| -v, --version | Displays version of the program |
| -h, --help | Displays a help message |
| -o, --output <output-location\> | Specifies the location to save converted files, creates a new folder if location doesn't exist, defaults to textml/ |
| -l, --lang | Specifies a language for the <html lang=...> element in the <html> root element |

## Examples

Expand Down Expand Up @@ -140,6 +141,10 @@ CPX = compare THE x register

# Changelog

**0.1.7**
- Added Optional HTML file Language specification
- Updates the `<html lang=...>` attribute in the root `<html>` element. If no language is provided `en-CA` is used by default.

**0.1.6:** (Based on Feedback)
- Updated argument handling to use argparse
- Simplified directory deletion using shutil
Expand All @@ -150,7 +155,6 @@ CPX = compare THE x register
0.1.5: (merged pull request from [rabroldan](https://github.com/rabroldan))
- Added **Markdown file support**
- Markdown syntax features, such as italics, bold, headings (Heading 1 and Heading 2), and links.

- Can correctly format these elements when converting Markdown to HTML.
- If you specify a single Markdown file it processes that file and saves the HTML output in the "textml" directory.
- If you run the script with a directory path it processes any Markdown files and text files within that directory and saves the generated HTML files in the "textml" directory or other specified directory.
Expand Down
12 changes: 6 additions & 6 deletions convertUtils.py
@@ -1,6 +1,6 @@
import os

def convertText(filePath, outDir):
def convertText(filePath, outDir, language):
fo = open(filePath, "r") # Opening file to process for writing

file = os.path.basename(filePath) # Getting basename of file
Expand All @@ -27,7 +27,7 @@ def convertText(filePath, outDir):

line = fo.readline() # Reading next line

finishedFile = write_html_content(HTMLContent, title)
finishedFile = write_html_content(HTMLContent, title, language)
fw.write(finishedFile)

fo.close() # Closing filestreams
Expand All @@ -47,10 +47,10 @@ def parseTitle(openFile, fileName):
return title

# process MD files
def write_html_content(new_content, new_title): # this provides the layout of the html with an editable title, content
def write_html_content(new_content, new_title, language): # this provides the layout of the html with an editable title, content

html_content = f"""<!DOCTYPE html>
<html lang="en">
<html lang="{language}">
<head>
<meta charset="utf-8">
<title>{new_title}</title>
Expand Down Expand Up @@ -137,7 +137,7 @@ def markdown_to_html_links(content):

return content

def convertMD(userInput, outDir):
def convertMD(userInput, outDir, language):
# Create the output directory if it doesn't exist
if not os.path.exists(outDir):
os.makedirs(outDir)
Expand All @@ -156,7 +156,7 @@ def convertMD(userInput, outDir):
body = markdownfeat(input_file, input_md_file)
# Convert the Markdown body to HTML and write it to the output HTML file

content = write_html_content(body, title)
content = write_html_content(body, title, language)

with open(output_html_file, 'w') as output_file:
output_file.write(content)
13 changes: 9 additions & 4 deletions textml.py
Expand Up @@ -17,29 +17,34 @@ def directory_setup(dir):

def main():
outDir = "til/" # Setting default output directory
lang = "en-CA"

# Argument parsing below
parser = argparse.ArgumentParser(description="Program accepts any .txt/.md file or a folder/directory of .txt/.md files and converts them to HTML files for use in webpages.")

parser.add_argument('-v', '--version', action='version', version="textml " + Metadata.version)
parser.add_argument('input', metavar='input', type=str, help="The provided .txt/.md file or a folder/directory of .txt/.md files to be converted")
parser.add_argument('-o','--output', metavar='output', type=str, help="Optionally specifies a directory to save converted HTML files (Creates folder if one does not exist")
parser.add_argument('-l','--lang', metavar='lang', type=str, help="Specifies a language for the <html lang=...> element in the <html> root element")

args = parser.parse_args()

if args.output:
outDir = args.output

if args.lang:
lang = args.lang

userInput = args.input

if (os.path.isdir(outDir) == False) or (outDir == "til/"):
directory_setup(outDir) # Clearing and remaking directory

if userInput.find(".") != -1: # Checking if the passed argument is a file
if ".txt" in userInput:
utils.convertText(userInput, outDir)
utils.convertText(userInput, outDir, lang)
elif ".md" in userInput: #if the file is md and output in the right directory
utils.convertMD(userInput, outDir)
utils.convertMD(userInput, outDir, lang)
else:
raise Exception("Error!: Invalid file type")

Expand All @@ -51,9 +56,9 @@ def main():
currFile = os.path.join(userInput, file)

if ".txt" in file: # Checking if each file's type is supported before converting
utils.convertText(currFile, outDir)
utils.convertText(currFile, outDir, lang)
elif ".md" in file: # Checking if each file's type is supported before converting
utils.convertMD(currFile, outDir)
utils.convertMD(currFile, outDir, lang)
else:
print("Error!: Invalid file type for: " + file)
else:
Expand Down

0 comments on commit c1e4bff

Please sign in to comment.