238 lines
7.9 KiB
Python
238 lines
7.9 KiB
Python
from markdown2 import markdown_path
|
|
from typing import List, Tuple
|
|
from datetime import date
|
|
from os import walk, system
|
|
|
|
|
|
markdown_dir = 'markdown'
|
|
html_dir = 'templates'
|
|
img_dir = 'images'
|
|
tab = ''
|
|
|
|
|
|
#################################
|
|
# Build Stage #
|
|
#################################
|
|
|
|
def build() -> None:
|
|
"""
|
|
Build all files needed for the web site.
|
|
|
|
Build HTML files from Markdown files.
|
|
Copy images from Markdown directory to HTML directory.
|
|
Build events template.
|
|
"""
|
|
md_files = get_sources()
|
|
create_html_files(md_files)
|
|
copy_files(md_files)
|
|
create_templates(md_files)
|
|
|
|
exit(0)
|
|
|
|
|
|
def change_img_url(html: str, html_path: str) -> str:
|
|
"""
|
|
Change the URL from each image tag inside the HTML code. The change is done only if it is a local address.
|
|
|
|
:param html: The original HTML code
|
|
:type html: str
|
|
:param html_path: Path of the HTML code
|
|
:type html_path: str
|
|
:return: The modify HTML code
|
|
:rtype: str
|
|
|
|
:Example:
|
|
|
|
>>> change_img_url('<img src="images/img.png" />', '13-04-2019-Example')
|
|
'<img src="templates/13-04-2019-Example/images/img.png" />'
|
|
>>> change_img_url('<img src="http://example.com" />', '13-04-2019-Example')
|
|
'<img src="http://example.com" />'
|
|
"""
|
|
img_pos = html.find("<img ")
|
|
if img_pos != -1:
|
|
new_html = html[:img_pos]
|
|
else:
|
|
new_html = html
|
|
|
|
while img_pos != -1:
|
|
src_pos = html.find("src=", img_pos+5)
|
|
src_end = html.find(html[src_pos + 4], src_pos + 5)
|
|
src = html[src_pos+5:src_end]
|
|
if src[:4] != 'http':
|
|
src = html_path[:html_path.rfind('/')] + '/' + src
|
|
new_html += html[img_pos:src_pos+5] + src
|
|
|
|
img_pos = html.find("<img ", src_end)
|
|
if img_pos != -1:
|
|
new_html += html[src_end:img_pos]
|
|
else:
|
|
new_html += html[src_end:]
|
|
|
|
return new_html
|
|
|
|
|
|
def copy_files(md_files: List[str]) -> None:
|
|
"""
|
|
Copy files and directories from Markdown directory to HTML directory.
|
|
|
|
:param md_files: List of all Markdown files path in the Markdown directory
|
|
:type md_files: List[str]
|
|
"""
|
|
for md_file in md_files:
|
|
directory = md_file[:md_file.rfind('/')]
|
|
system('cp -r ' + directory.replace(' ', '\\ ') + '/* ' + directory.replace(' ', '_').replace(markdown_dir, html_dir))
|
|
rm_file = md_file.replace(markdown_dir, html_dir).replace(' ', '\\ ')
|
|
rm_file = rm_file[:rm_file.rfind('/')+1].replace('\\ ', '_') + rm_file[rm_file.rfind('/')+1:]
|
|
system('rm ' + rm_file)
|
|
|
|
|
|
def create_html_files(md_files: List[str]) -> None:
|
|
"""
|
|
Build HTML files from Markdown files.
|
|
|
|
:param md_files: List of all Markdown files path in the Markdown directory
|
|
:type md_files: List[str]
|
|
"""
|
|
for md_file in md_files:
|
|
original_html = markdown_path(md_file) # Conversion from markdown to HTML
|
|
html_directory = html_dir + '/' + md_file.split('/')[1].replace(' ', '_')
|
|
html_filename = md_file[:-3].replace(markdown_dir, html_dir).replace(' ', '_') + '.html'
|
|
system('mkdir ' + html_directory) # Creation of the HTML directory
|
|
final_html = change_img_url(original_html, html_filename)
|
|
|
|
title = md_file[md_file.rfind('/') + 1:].split('-')[3][:-3]
|
|
event_date = md_file[md_file.rfind('/')+1:][:10].replace('-', '/')
|
|
final_html = '<h2 class="major"><div class="title">' + title + '</div><div class="date">' + event_date + \
|
|
'</div></h2>\n' + final_html
|
|
|
|
with open(html_filename, 'w', encoding='utf-8') as html_file:
|
|
html_file.write(final_html) # Write HTML inside the file
|
|
|
|
|
|
def create_templates(md_files: List[str]) -> None:
|
|
"""
|
|
Build events and index template.
|
|
|
|
:param md_files: List of all Markdown files path in the Markdown directory
|
|
:type md_files: List[str]
|
|
"""
|
|
events_html = '<h2 class="major">Évènements et foyers</h2>\n'
|
|
index_html = ''
|
|
if len(md_files) != 0:
|
|
for md_file in md_files:
|
|
event_date, title, event = parse_md_filename(md_file.replace(' ', '_'))
|
|
if (event_date, date, title) == (date.min, '', ''):
|
|
continue
|
|
|
|
events_html += '<a class="event" href="#event-' + event_date.strftime('%d-%m-%Y') + '-' + title + '">\n \
|
|
<div class="date">' + event_date.strftime('%a %d %b %Y') + '</div>\n \
|
|
<div class="title">' + title.replace('_', ' ') + '</div>\n \
|
|
<div class="description">\n' + event + '</div>\n \
|
|
</a>\n'
|
|
|
|
index_html += '<article id="event-' + event_date.strftime(
|
|
'%d-%m-%Y') + '-' + title + '"></article>\n\t\t\t\t'
|
|
else:
|
|
events_html += "<p>Pas d'évènements prévus à ce jour :(</p>\n"
|
|
|
|
with open('templates/events.html', 'w', encoding='utf-8') as events_template:
|
|
events_template.write(events_html)
|
|
|
|
initial_index_html = ''
|
|
with open("index.html", "r", encoding='utf-8') as index_template:
|
|
for line in index_template:
|
|
initial_index_html += line
|
|
|
|
with open("index.html", "w", encoding='utf-8') as index_template:
|
|
position = initial_index_html.find('<!-- Events articles -->')
|
|
index_template.write(initial_index_html[:position] + index_html + initial_index_html[position:])
|
|
|
|
|
|
def del_links(html: str) -> str:
|
|
"""
|
|
Delete the link tags (<a>) inside of the HTML code.
|
|
|
|
:param html: HTML code with link tags
|
|
:type html: str
|
|
:return: HTML code without link tags
|
|
:rtype: str
|
|
|
|
:Example:
|
|
|
|
>>> del_links('<p>An <a href="http://example.com">example</a></p>')
|
|
'<p>An example</p>'
|
|
"""
|
|
new_html = ""
|
|
i = 0
|
|
opened = False
|
|
while i < len(html) - 4: # Not going out of range
|
|
if html[i:i + 3] == "<a ": # Beginning of an opening link tag
|
|
for j in range(3, len(html) - i - 3):
|
|
if html[i + j] == ">": # Ending of the opening link tag
|
|
i += j + 1
|
|
break
|
|
opened = True
|
|
elif html[i:i + 4] == "</a>" and opened: # Closing link tag
|
|
i += 4
|
|
opened = False
|
|
else:
|
|
new_html += html[i]
|
|
i += 1
|
|
|
|
return new_html
|
|
|
|
|
|
def get_sources() -> List[str]:
|
|
"""
|
|
Search and return markdown files path used as sources.
|
|
|
|
:return: The markdown files
|
|
:rtype: List[str]
|
|
"""
|
|
md_files = []
|
|
for (root, dirs, files) in walk(markdown_dir):
|
|
for file in files:
|
|
if file[-3:] == '.md': # It is a markdown file
|
|
md_files.append(root + '/' + file)
|
|
|
|
return md_files
|
|
|
|
|
|
def parse_md_filename(filename: str) -> Tuple[date, str, str]:
|
|
"""
|
|
Parse the Markdown file to have the event date, the title and the description.
|
|
|
|
:param filename: Filename of the Markdown file.
|
|
Format must be: day-month-year-title.md
|
|
:type filename: str
|
|
:return: If the filename format is correct: (Event Date, Title, Description)
|
|
If it is not correct: (date.min, '', '')
|
|
:rtype: Tuple[date, str, str]
|
|
|
|
:Example:
|
|
|
|
>>> parse_md_filename('13-06-2019-Example.md')
|
|
(date(2019, 06, 13), Example, *Content of the Markdown file without link tags*)
|
|
>>> parse_md_filename('13-06-2019-BadExample')
|
|
(date.min, '', '')
|
|
>>> parse_md_filename('Bad-Example')
|
|
(date.min, '', '')
|
|
"""
|
|
split = filename[filename.rfind('/') + 1:].split('-')
|
|
if len(split) == 4: # Format is good : day_month_year_title.md
|
|
day, month, year, title = split
|
|
title = title[:-3] # Remove the extension
|
|
event_date = date(int(year), int(month), int(day))
|
|
else:
|
|
return date.min, '', ''
|
|
|
|
event = ''
|
|
nb_line = 0
|
|
with open(filename.replace(markdown_dir, html_dir).replace('.md', '.html'), 'r', encoding='utf-8') as html_file:
|
|
for line in html_file:
|
|
if nb_line > 0:
|
|
event += line
|
|
nb_line += 1
|
|
event = del_links(event)
|
|
|
|
return event_date, title, event
|