import os
import sys
import io
import shutil
import configparser
import markdown

# Helper functions to read and write file contents
def readFile ( file ):
	reader = io.open(file, 'r', encoding='utf8')
	content = reader.read()
	reader.close()
	return content

def writeFile ( file, content ):
	writer = io.open(file, 'w', encoding='utf8')
	writer.write(content)
	writer.close()

def vprint(text):
	if "-v" in sys.argv:
		print(text)

def readConfig():
	global title
	global baseurl
	config_file = configparser.ConfigParser()
	config_file.read("input/configuration.ini")
	title = config_file['pswg']['title']
	baseurl = config_file['pswg']['baseurl']

def prepareOutput():
	# Delete old content and prepare output folder
	if os.path.isdir("output"):
		shutil.rmtree("output")
	os.mkdir("output")
	os.mkdir("output/page")
	os.mkdir("output/article")
	os.mkdir("output/theme")
	
	shutil.copy("input/theme/style.css", "output/theme/style.css")
	shutil.copytree("input/img", "output/img")

def importTemplates():
	global pageTemplate
	global articleTemplate
	pageTemplate = readFile("input/theme/templates/page.html")
	articleTemplate = readFile("input/theme/templates/article.html")

def parseStructureElements():
	global head
	global header
	global navigation
	global footer
	
	head = readFile("input/theme/head.html").replace('{% TITLE %}', title)
	
	# Read page header, transform to HTML and add a link to the base URL to the page title
	header = markdown.markdown(readFile('input/static/header.md'))
	header = header.replace('<h1>', '<h1><a href="' + baseurl + '">')
	header = header.replace('</h1>', '</a></h1>')
	
	# Read the navigation, transform to HTML and add the baseURL to all links
	navigation = markdown.markdown(readFile('input/static/navigation.md'))
	navigation = navigation.replace('href="', 'href="' + baseurl)
	
	# Read the footer, transform to HTML
	footer = markdown.markdown(readFile('input/static/footer.md'))

def replacePlaceholders(template, content):
	text = template
	text = text.replace('{% HEAD %}', head)
	text = text.replace('{% HEADER %}', header)
	text = text.replace('{% NAVIGATION %}', navigation)
	text = text.replace('{% FOOTER %}', footer)
	text = text.replace('{% MAIN %}', content)
	text = text.replace('{% BASEURL %}', baseurl)
	return text

# Main script
readConfig()
prepareOutput()
importTemplates()
parseStructureElements()

# Create page files
vprint ("# Create pages")
for file in os.listdir('input/page'):
	vprint("- " + file)

	# Read the pages content markdown and convert it into html
	content = readFile('input/page/' + file)
	content = markdown.markdown(content)
	
	# Concatenate the output
	outputString = replacePlaceholders(pageTemplate, content)

	fileName = os.path.splitext(file)[0]
	writeFile ("output/page/" + fileName + ".html", outputString)


# Create article files
articleOverview = ""
vprint (os.linesep + "# Create articles")
for file in os.listdir('input/article'):
	vprint("- " + file)
	# Read the articles content markdown and convert it into html
	articleContent = readFile('input/article/' + file)
	articleContent = markdown.markdown(articleContent)

	fileName = os.path.splitext(file)[0]
	shortFileName = fileName.split(" ", 2)[1]

	# Concatenate the output
	outputString = replacePlaceholders(articleTemplate, articleContent)

	outputString = outputString.replace('[[READMORE]]', '')

	writeFile ("output/article/" + shortFileName + ".html", outputString)
	
	articleContent = articleContent.replace('<h1>', '<h1><a href="' + baseurl + 'article/' + shortFileName + '.html">')
	articleContent = articleContent.replace('</h1>', '</a></h1>')
	articleContent = articleContent.split('[[READMORE]]')[0]

	articleOverview += '<article>' + articleContent + '&rarrhk; <a href="' + baseurl + 'article/' + shortFileName + '.html">Weiterlesen</a></article>'
	

outputString = pageTemplate
	
outputString = outputString.replace('{% HEAD %}', head)
outputString = outputString.replace('{% HEADER %}', header)
outputString = outputString.replace('{% NAVIGATION %}', navigation)
outputString = outputString.replace('{% FOOTER %}', footer)
outputString = outputString.replace('{% MAIN %}', articleOverview)
outputString = outputString.replace('{% BASEURL %}', baseurl)

writeFile ("output/index.html", outputString)
	
	
	
	
	
	
	