Commit 7d0130c3 authored by Matthieu Boileau's avatar Matthieu Boileau

Some refactoring on Article* classes

parent e8454f22
......@@ -47,9 +47,9 @@ logger.addHandler(ch)
class SpipToMarkdown:
"""A class to export spip article format to a markdown Pelican article"""
def __init__(self, website):
"""A generic class to export spip format to a markup language"""
self.website = website
def convert(self, s):
......@@ -295,8 +295,10 @@ class SpipToMarkdown:
class SpipToRst(SpipToMarkdown):
"""A class to export spip article format to a ReStructuredText Pelican article"""
def convert(self, s, preserve_line_breaks=False):
"""Apply a pandoc conversion to markdown format"""
s = super().convert(s)
if preserve_line_breaks:
extra_args = ['--wrap=preserve']
......@@ -308,7 +310,7 @@ class SpipToRst(SpipToMarkdown):
class Article:
"""A single Spip article or rubrique to be converted into Pelican"""
"""A generic class for a single Spip article or rubrique to be converted into a Pelican article file"""
def __init__(self, spip_article, spip_type, website):
......@@ -365,53 +367,14 @@ class Article:
self.tags = [self.type]
if self.website.ml_type == 'md':
# Instanciate a spip -> markdown translator
s2md = SpipToMarkdown(self.website)
self.convert = s2md.convert
def convert_title(title):
return self.convert(title).strip() # strip to remove any CR at end of string
self.convert_title = convert_title
self.get_header = self.get_header_markdown
elif self.website.ml_type == 'rst':
# Instanciate a spip -> rst translator
s2rst = SpipToRst(self.website)
self.convert = s2rst.convert
def convert_title(title):
return self.convert(title, preserve_line_breaks=True).strip()
self.convert_title = convert_title
self.get_header = self.get_header_rst
else:
exit(f'Unknown markup language type: {self.website.ml_type}!')
s2md = SpipToMarkdown(self.website)
self.convert = self.get_converter()
def get_header_markdown(self):
header = f"""\
title: {self.title}
date: {self.date}
modified: {self.modified}
category: {self.category}
tags: {self.tags}
slug: {self.prefix}
authors: {self.authors}
summary: {self.summary}
"""
return header
def get_header_rst(self):
print(self.title)
title = f"{self.title}\n{'#'*len(self.title)}\n\n"
header = title + f"""\
:date: {self.date}
:modified: {self.modified}
:category: {self.category}
:tags: {self.tags}
:slug: {self.prefix}
:authors: {self.authors}
:summary: {self.summary}
def get_header(self):
pass
"""
return header
def get_converter(self):
pass
def export_to_pelican(self):
"""
......@@ -445,6 +408,59 @@ summary: {self.summary}
return self.skip_reason
class ArticleMd(Article):
"""A single Spip article or rubrique to be converted into Pelican"""
def get_converter(self):
"""Return a Markdown converter"""
s2ml = SpipToMarkdown(self.website) # Instanciate a spip -> markdown translator
return s2ml.convert
def convert_title(self, title):
return self.convert(title).strip() # strip to remove any CR at end of string
def get_header(self):
header = f"""\
title: {self.title}
date: {self.date}
modified: {self.modified}
category: {self.category}
tags: {self.tags}
slug: {self.prefix}
authors: {self.authors}
summary: {self.summary}
"""
return header
class ArticleRst(Article):
"""A single Spip article or rubrique to be converted into Pelican"""
def get_converter(self):
"""Return a Markdown converter"""
s2ml = SpipToRst(self.website) # Instanciate a spip -> rst translator
return s2ml.convert
def convert_title(self, title):
return self.convert(title, preserve_line_breaks=True).strip()
def get_header(self):
print(self.title)
title = f"{self.title}\n{'#'*len(self.title)}\n\n"
header = title + f"""\
:date: {self.date}
:modified: {self.modified}
:category: {self.category}
:tags: {self.tags}
:slug: {self.prefix}
:authors: {self.authors}
:summary: {self.summary}
"""
return header
class Website:
"""Define a website from Spip data"""
......@@ -463,6 +479,7 @@ class Website:
self.reset_output_dir = reset_output_dir
self.include_breves = include_breves
self.ml_type = ml_type
self.rubrique_to_category = {}
self.article_index = {}
self.doc_index = {}
......@@ -518,6 +535,13 @@ class Website:
if self.include_breves:
self.categories['breves'] = get_categories('breves')
if self.ml_type == 'md':
self.Article = ArticleMd
elif self.ml_type == 'rst':
self.Article = ArticleRst
else:
exit(f'Unknown markup language type: {self.ml_type}!')
def _reset_output_directories(self):
"""Erase existing output files and create empty output directories"""
if os.path.exists("content"):
......@@ -631,7 +655,7 @@ class Website:
with open(filename, mode='r') as yml_file:
spip_articles = yaml.load(yml_file)
for spip_article in spip_articles:
article = Article(spip_article, spip_type, self)
article = self.Article(spip_article, spip_type, self)
self.articles.append(article)
if not article.skip_reason:
self.article_index[article.id] = article.path
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment