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

Markdown support #5

Merged
merged 6 commits into from Sep 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 7 additions & 1 deletion README.md
Expand Up @@ -8,7 +8,7 @@ Author: Stefan Frunza
<p><strong>THIS PROGRAM DELETES THE TARGET DIRECTORY SPECIFIED WITH THE -o OPTION.</strong> it then recreates it and populates it with the new HTML, but remember <strong>THIS PROGRAM DELETES THE TARGET DIRECTORY SPECIFIED WITH THE -o OPTION.</strong></p>

<H1>What it is</H1>
<p>A tool that will take a text file as an option and create HTML markup based upon it.<br>
<p>A tool that will take a text (.txt) or markdown (.md) file as an option and create HTML markup based upon it.<br>
To use simply open a console to the script's location and write <strong>python Sitegen.py</strong> along with the options you wish to specify, among which you must have a plain text file to convert to mark up.</p>


Expand All @@ -31,3 +31,9 @@ To use simply open a console to the script's location and write <strong>python S
| -i | --input | Specify an Input directory or file only .txt suffix will be correctly parsed (requires argument) |
| -o | --ouput | Specify a name for existing directory (optional argument)|

<h2>Markdown Support:</h2>

| Type | Or | Result |
| -------- | -------- | ------ |
| \*Italic\* | \_Italic\_ | <i>Italic</i> |
| \*\*Bold\*\* | \_\_Bold\_\_ | <b>Bold</b> |
47 changes: 34 additions & 13 deletions SSJ.py
@@ -1,5 +1,6 @@
from os import listdir
from os.path import isfile, join
import re

class SSJ:
defaultOutputFolder = "./dist"
Expand Down Expand Up @@ -37,7 +38,7 @@ def parseFile(self, inputFile, inputFolder = None):


Lines = file.readlines()

for i, line in enumerate(Lines):
#my logic doesnt work. I need it to be blank line delimiter.
if i == 0:
Expand All @@ -46,32 +47,32 @@ def parseFile(self, inputFile, inputFolder = None):
if line != "\n":
titleQuestionMark = False
newLine = "<p>" + titleStorage
paragraphs.append(newLine)
paragraphs.append(SSJ.convertMarkdown(newLine) if inputFile.endswith(".md") else newLine)
newLine = line
paragraphs.append(newLine)
paragraphs.append(SSJ.convertMarkdown(newLine) if inputFile.endswith(".md") else newLine)
elif i == 2:
if line != "\n":
titleQuestionMark = False
newLine = "<p>" + titleStorage + "</p>"
paragraphs.append(newLine)
paragraphs.append(SSJ.convertMarkdown(newLine) if inputFile.endswith(".md") else newLine)
newLine = "<p>" + line
paragraphs.append(newLine)
paragraphs.append(SSJ.convertMarkdown(newLine) if inputFile.endswith(".md") else newLine)
elif i == 3:
if titleQuestionMark == True:
newLine = "<h1>" + titleStorage + "</h1>"
paragraphs.append(newLine)
paragraphs.append(SSJ.convertMarkdown(newLine) if inputFile.endswith(".md") else newLine)
newLine = "<p>" + line
paragraphs.append(newLine)
paragraphs.append(SSJ.convertMarkdown(newLine) if inputFile.endswith(".md") else newLine)
else:
if line == "\n":
newLine = "</p>" + "<p>"
paragraphs.append(newLine)
paragraphs.append(SSJ.convertMarkdown(newLine) if inputFile.endswith(".md") else newLine)
else:
newLine = line
paragraphs.append(newLine)
paragraphs.append(SSJ.convertMarkdown(newLine) if inputFile.endswith(".md") else newLine)
#print (paragraphs)

outputName = inputFile[:inputFile.find('.txt')] + '.html'
outputName = inputFile[:inputFile.find('.')] + '.html'

print("new name:", outputName)

Expand Down Expand Up @@ -115,9 +116,9 @@ def parseDir(self, input):
SSJ.template = SSJ.template.replace("Filename", "Index Page")

for file in onlyfiles:
if file.endswith(".txt"):
if file.endswith(".txt") or file.endswith(".md"):
self.parseFile(file, inputFolder)
outputName = (file[:file.find('.txt')] + '.html')
outputName = (file[:file.find('.')] + '.html')
hrefName = outputName.replace(" ", "%20")
pos = SSJ.template.find(SSJ.token) + len(SSJ.token)
SSJ.template = SSJ.template[:pos] + "<a href={}>{}</a><br>".format(hrefName, outputName) + SSJ.template[pos:]
Expand All @@ -130,4 +131,24 @@ def parseDir(self, input):
except OSError as err:
print("Error: " + str(err))

SSJ.template = temp
SSJ.template = temp

def markdownSearch(regex, indChars, tag, line):
newLine = line
match = re.search(regex,newLine)
while match != None:
newLine = newLine[:match.span()[0]] + "<" + tag + ">" + newLine[match.span()[0]+indChars:match.span()[1]-indChars] + "</"+ tag +">" + newLine[match.span()[1]:]
match = re.search(regex,newLine)
return newLine

def convertMarkdown(line):
newLine = line
#bold
newLine = SSJ.markdownSearch("\*\*[^*]+\*\*", 2, "b", newLine)
newLine = SSJ.markdownSearch("__[^*]+__", 2, "b", newLine)
#italics
newLine = SSJ.markdownSearch("\*[^*]+\*", 1, "i", newLine)
newLine = SSJ.markdownSearch("_[^*]+_", 1, "i", newLine)


return newLine
2 changes: 1 addition & 1 deletion Sitegen.py
Expand Up @@ -37,7 +37,7 @@ def main(argv):

os.mkdir(SiteGen.output)

if SiteGen.input.endswith(".txt"):
if SiteGen.input.endswith(".txt") or SiteGen.input.endswith(".md") :
SiteGen.parseFile(input)
elif isdir(SiteGen.input):
SiteGen.parseDir(input)
Expand Down