started modularization
This commit is contained in:
parent
c91c5396fd
commit
92de47d8cd
5
content.py
Normal file
5
content.py
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
class content:
|
||||||
|
def __init__(self, content):
|
||||||
|
self.content = content
|
||||||
|
def get_markdown(self):
|
||||||
|
return self.content
|
27
main.py
27
main.py
@ -5,10 +5,14 @@ import sys
|
|||||||
from pprint import pprint
|
from pprint import pprint
|
||||||
# from peewee import *
|
# from peewee import *
|
||||||
import pymysql
|
import pymysql
|
||||||
from slugify import slugify
|
# Local modules
|
||||||
import yaml
|
from metadata import metadata
|
||||||
|
from content import content
|
||||||
|
|
||||||
|
# Constants definition
|
||||||
outputDir = "output"
|
outputDir = "output"
|
||||||
|
outputType = "md"
|
||||||
|
|
||||||
# Clean the output dir & create a new
|
# Clean the output dir & create a new
|
||||||
shutil.rmtree(outputDir, True)
|
shutil.rmtree(outputDir, True)
|
||||||
os.mkdir(outputDir)
|
os.mkdir(outputDir)
|
||||||
@ -25,12 +29,7 @@ db = pymysql.connect(
|
|||||||
cursor = db.cursor()
|
cursor = db.cursor()
|
||||||
cursor.execute("SELECT * FROM spip_articles ORDER BY date DESC")
|
cursor.execute("SELECT * FROM spip_articles ORDER BY date DESC")
|
||||||
|
|
||||||
# Loop through the results and format data into Markdown files
|
# Choose how many articles export based on first param
|
||||||
# Columns:
|
|
||||||
# 2: titre
|
|
||||||
# 7: texte
|
|
||||||
# 9: date
|
|
||||||
|
|
||||||
if len(sys.argv) > 1:
|
if len(sys.argv) > 1:
|
||||||
if int(sys.argv[1]) > 0:
|
if int(sys.argv[1]) > 0:
|
||||||
fetch = cursor.fetchmany(int(sys.argv[1]))
|
fetch = cursor.fetchmany(int(sys.argv[1]))
|
||||||
@ -41,12 +40,14 @@ else:
|
|||||||
print("--- {} articles will be converted to Markdown files wihth YAML metadata ---\n".format(len(fetch)))
|
print("--- {} articles will be converted to Markdown files wihth YAML metadata ---\n".format(len(fetch)))
|
||||||
pprint(fetch)
|
pprint(fetch)
|
||||||
|
|
||||||
|
# Loop among every articles & export them in Markdown files
|
||||||
for row in fetch:
|
for row in fetch:
|
||||||
frontmatter = {"title": row[2], "date": row[9]}
|
meta = metadata(row)
|
||||||
content = "---\n{}---\n# {}\n\n{}".format(yaml.dump(frontmatter), row[2], row[7])
|
body = content(row[7])
|
||||||
path = "{}/{}.md".format(outputDir, slugify("{}-{}".format(row[0], row[2])))
|
with open("{}/{}.{}".format(outputDir, meta.get_slug(), outputType), "w") as f:
|
||||||
with open(path, "w") as f:
|
f.write("{}\n{}\n{}".format(
|
||||||
f.write(content)
|
meta.get_frontmatter(), meta.get_title(), body.get_markdown())
|
||||||
|
)
|
||||||
|
|
||||||
# Close the database connection
|
# Close the database connection
|
||||||
db.close()
|
db.close()
|
||||||
|
19
metadata.py
Normal file
19
metadata.py
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
import yaml
|
||||||
|
from slugify import slugify
|
||||||
|
|
||||||
|
class metadata:
|
||||||
|
# row is an array containing at positions :
|
||||||
|
# 0: id
|
||||||
|
# 2: title (titre)
|
||||||
|
# 7: body (texte)
|
||||||
|
# 9: date
|
||||||
|
def __init__(self, row):
|
||||||
|
self.id = row[0]
|
||||||
|
self.title = row[2]
|
||||||
|
self.date = row[9]
|
||||||
|
def get_title(self):
|
||||||
|
return "# {}\n".format(self.title)
|
||||||
|
def get_slug(self):
|
||||||
|
return slugify("{}-{}".format(self.id, self.title))
|
||||||
|
def get_frontmatter(self):
|
||||||
|
return "---\n{}---".format(yaml.dump({"title": self.title, "date": self.date}))
|
Loading…
Reference in New Issue
Block a user