Checkpoint: OPML import

This commit is contained in:
Cam Stevenson 2017-10-21 16:29:17 -04:00
parent 56553cf119
commit bdc6a096f2

View file

@ -7,7 +7,6 @@ from manuskript.models.outlineModel import outlineItem
from manuskript.enums import Outline
import xmltodict
from manuskript.functions import mainWindow
import xml.etree.ElementTree as ET
def handleCard(_, card):
@ -20,12 +19,12 @@ def exportOpml():
def importOpml(opmlFilePath):
with open(opmlFilePath, 'r') as opmlFile:
opmlContent = opmlFile.read()
opmlContent = saveNewlines(opmlFile.read())
mw = mainWindow()
mdl = mw.mdlOutline
dict = xmltodict.parse(opmlContent, item_callback=handleCard)
dict = xmltodict.parse(opmlContent, strip_whitespace=False)
opmlNode = dict['opml']
bodyNode = opmlNode['body']
@ -33,12 +32,42 @@ def importOpml(opmlFilePath):
outline = bodyNode['outline']
for element in outline:
if '@text' in element:
card = outlineItem(parent=mdl.rootItem)
card.title = element['@text']
card.ID = card.title
card.path = ''
if '@_note' in element:
card.setData(Outline.text.value, element['@_note'])
parseItems(underElement=element, parentItem=mdl.rootItem)
return True
def parseItems(underElement, parentItem):
if '@text' in underElement:
card = outlineItem(parent=parentItem, title=underElement['@text'])
text = ""
summary = ""
if '@_note' in underElement:
text = restoreNewLines(underElement['@_note'])
summary = text[0:128]
card.setData(Outline.summaryFull.value, summary)
if 'outline' in underElement:
elements = underElement['outline']
for el in elements:
parseItems(el, card)
else:
card.setData(Outline.type.value, 'md')
card.setData(Outline.text.value, text)
# I assume I don't have to do the following
# parentItem.appendChild(card)
return
def saveNewlines(inString):
inString = inString.replace("\r\n", "\n")
inString = inString.replace("\n", "{{lf}}")
return inString
def restoreNewLines(inString):
return inString.replace("{{lf}}", "\n")