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('', '13-04-2019-Example') '' >>> change_img_url('', '13-04-2019-Example') '' """ img_pos = html.find(" 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 = '

' + title + '
' + event_date + \ '

\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 = '

Évènements et foyers

\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 += '\n \
' + event_date.strftime('%a %d %b %Y') + '
\n \
' + title.replace('_', ' ') + '
\n \
\n' + event + '
\n \
\n' index_html += '
\n\t\t\t\t' else: events_html += "

Pas d'évènements prévus à ce jour :(

\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('') index_template.write(initial_index_html[:position] + index_html + initial_index_html[position:]) def del_links(html: str) -> str: """ Delete the link tags () 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('

An example

') '

An example

' """ new_html = "" i = 0 opened = False while i < len(html) - 4: # Not going out of range if html[i:i + 3] == "": # Ending of the opening link tag i += j + 1 break opened = True elif html[i:i + 4] == "" 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