Create a gist now

Instantly share code, notes, and snippets.

Skrypt konwertujący XML z danymi wpisów wyeksportowanymi z panelu administracyjnego Joggera na pliki .markdown gotowe do katalogu postów w Octopresie
import re
from xml.dom import minidom
from html.parser import HTMLParser
XMLFILENAME = 'jogger_eksport.xml'
def createFileName(date, permalink, subject):
"""Data wymagana, w formacie YYYY-mm-dd"""
fileName = date[:10] + '-'
if permalink != None:
fileName += permalink
else:
fileName += re.sub(r'[^a-zA-Z\d\.-]', '_', subject.lower())
return fileName + '.markdown'
DOMTree = minidom.parse(XMLFILENAME)
cNodes = DOMTree.childNodes
postNo = 1
for i in cNodes[0].getElementsByTagName("entry"):
date = i.getElementsByTagName("date")[0].childNodes[0].toxml()
subject = 'Bez tytułu ' + str(postNo)
postNo = postNo + 1
if i.getElementsByTagName("subject")[0].firstChild != None:
subject = i.getElementsByTagName("subject")[0].firstChild.toxml()
permalink = None
if i.getElementsByTagName("permalink")[0].firstChild != None:
permalink = i.getElementsByTagName("permalink")[0].firstChild.toxml()
fileName = createFileName(date, permalink, subject)
body = ''
if i.getElementsByTagName("body")[0].firstChild != None:
body = i.getElementsByTagName("body")[0].firstChild.toxml()
body = HTMLParser().unescape(body).replace('<EXCERPT>', '\n<!-- more -->\n')
categories = []
for cat in i.getElementsByTagName("category"):
categories.append(cat.firstChild.nodeValue)
text_file = open(fileName, "w", encoding="utf-8")
text_file.write('---\n')
text_file.write('layout: post\n')
text_file.write('title: "' + subject + '"\n')
text_file.write('date: ' + date[:16] + '\n')
text_file.write('comments: true\n')
text_file.write('categories:\n')
for cat in categories:
text_file.write('- ' + cat + '\n')
text_file.write('---\n')
text_file.write(body)
text_file.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment