Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add initial support for Markdown #9

Merged
merged 4 commits into from
Sep 22, 2023
Merged

Add initial support for Markdown #9

merged 4 commits into from
Sep 22, 2023

Conversation

rabroldan
Copy link
Contributor

Created a new funciton titled markdownfeat , write_MD_to_html ,markdown_to_html_links and convertMD

I did not change anything instead I added a new conditional statement in the main function

    if done != True or userInput.find(".") != -1:               # Checking if the file is md
        if ".md" in userInput:
            convertMD(userInput)
            done = True
        else: 
            Exception("Error!: Invalid file type")

this process the md files

convertMD - is the initial convertion MD files to html this takes

markdownfeat - is the process of changing the syntaxes of

  • italic
  • bold

markdown_to_html_links - is the process tjat propely place the link ito an HTML syntax

write_MD_to_html - creates the document ready to be written to the html file

@rabroldan
Copy link
Contributor Author

To test the file

python textml.py test.md

@ijacobs-cpa
Copy link
Owner

Thanks! Good work on this especially with how you included multiple markdown features (including links!) but there are a couple of problems that need addressing:

Initially, when testing this out with test.md it crashed with the following error:

$ python textml.py test.md
...
TypeError: markdownfeat() takes 1 positional argument but 2 were given

which traces to an incorrect function call in the convertMD() function here:

def markdownfeat(input_filename):
... 

def convertMD(userInput):
        new_title=userInput
        ...
        body = markdownfeat(new_title, html_newfile_path) # assign the newly created markdown feat to body to be assigned to the proper HTML format (ERROR HERE!)

The program works correctly after fixing it (removing html_newfile_path as its not used in convertMD()).


Another problem has to do with the HTML conversion. Your method of replacing each md styling is very good and efficient but when converting it forgets to use any <p> tags for regular text and so text remains untagged in the final HTML:

In your example:

<body>

  This is a markdown Test  <!--  not in a <p> tag -->

<em>The markdown test with an Italicized sentence</em><br><br>
<strong>The markdown test with a bold sentence and below is where the link is changed to match the style properly</strong><br><br> 
<a href=" (https://github.com/rabroldan/textml)">Link</a> <br><br>
</body>

Lastly, when the HTML file is created (using $ python textml.py test.md) it creates the file in the source directory when it should be created in the generated textml/ directory or a specified output directory as it would with normal text files (see my convertText() and default_dir_setup() functions for example)


A change that has to be made for your implementation is how your convertMD() function is called in the main(). The function doesn't allow for passed directories containing .md files to be converted like with text files.

In the main implementation, I assume 1 of 3 things could happen: a single file is passed (regardless of extension for now), a directory is passed, or a single command is issued.

When you call convertMD() in the main via:

 if done != True or userInput.find(".") != -1:               # Checking if the file is md
        if ".md" in userInput:
            convertMD(userInput)
            done = True
        else: 
            Exception("Error!: Invalid file type")

It adds a fourth option: convert a single .md file which should fall under converting a single file or a passed directory of files.

To properly correct this I ask you to put the if ".md" in userInput: convertMD(userInput) section in those first two options. For example:

 if done != True or userInput.find(".") != -1:               # Checking if the passed argument is a file
        if ".txt" in userInput:
            convertText(userInput, outDir)
            done = True
       elif ".md" in userInput: 
            convertMD(userInput)
            done = True
        else: 
            Exception("Error!: Invalid file type")

Having the same thing being updated within the directory handling (in the for loop) so that it could process a passed directory full of both .txt files and .md files.

After these issues and changes are fixed and implemented this pull request can be merged. Feel free to ask any more questions for clarification.

@rabroldan
Copy link
Contributor Author

rabroldan commented Sep 21, 2023

Markdown feature has been updated

it now takes all Markdown syntax features for [Italics, Bold, Heading 1, Heading 2, or a Link

It now output in the testml directory or if any specified directory

it is also processed in the examples directory if an md file is in the example

Example - for within directory md processing it still go to the testml directory

python textml.py examples

Example - for outside the directory md processing it still go to the testml directory

python textml.py mdtest.md

Example - for outside the directory md processing and specified directory it now go to that specified directory

python textml.py mdtest.md -o testmd

Features has now been updated and proper tags is created

@ijacobs-cpa
Copy link
Owner

Alright, this fixes all the issues that I mentioned before thanks. A couple more small changes and additions need to be made before it can be merged.

  1. Code Issue:

There is a double import (imports are at the top) on line 177 of my textml.py in 81033a9

No test files: There are no test files provided in the project. Please add a test4.md or equivalent file in the examples folder.

  1. Title parsing of .md files:

When a .md file is converted it should check if the file starts with text followed by 2 newlines and then set that as the title both in the HTML <body></body> and the header <title> as described in my README and implemented in my convertText() function here:

    fw = open(outDir + newHTMLFile + ".html", 'w')      # Writing new html file to output directory
    parseTitle = fo.readlines()[0:3]    # Reading first 3 lines for title

    if parseTitle[1] == '\n' and parseTitle[2] == '\n':
        fw.write(writeHTMLHeader(parseTitle[0].rstrip('\n')))       # HTML header as parsed title       
        fw.write("<h1>" + parseTitle[0].rstrip('\n') + '</h1>\n')   # Writing found title to html

        fo.seek(len(parseTitle[0]) + 2, 0)      # Reseting file position to right before title
    else:
        fw.write(writeHTMLHeader(newHTMLFile))       # HTML header as filename (for no title)
        fo.seek(0, 0)

This can be copied from the convertText() function (with the correct names) and put in one of your functions before reading the file for its contents so that it applies to the .md files like it does text files.

  1. README updates

Since we're adding a new feature it would need to be described in the README so please update it with new information regarding the support of .md files

After fixing these additional small issues and testing please update the issue-8 branch and if these are fixed this PR will be merged thanks.

@rabroldan
Copy link
Contributor Author

New update and corrected issues

  1. Code Issue: duplicate Import remove
  2. Test files was already included named mdtest.md located in the main directory or
  3. Title Parsing name of the document would be the first sentence
  4. Readme.Md updated

@ijacobs-cpa
Copy link
Owner

Fixes #8

@ijacobs-cpa ijacobs-cpa merged commit 830180c into ijacobs-cpa:main Sep 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants