def outlineFromMMD(text, parent): """ Creates outlineItem from multimarkdown file. @param text: content of the file @param parent: appends item to parent (outlineItem) @return: outlineItem """ item = outlineItem(parent=parent) md, body = parseMMDFile(text, asDict=True) # Store metadata for k in md: if k in Outline.__members__: item.setData(Outline.__members__[k].value, str(md[k])) # Store body item.setData(Outline.text.value, str(body)) # Set file format to "md" # (Old version of manuskript had different file formats: text, t2t, html and md) # If file format is html, convert to plain text: if item.type() == "html": item.setData(Outline.text.value, HTML2PlainText(body)) if item.type() in ["txt", "t2t", "html"]: item.setData(Outline.type.value, "md") return item
def outlineFromMMD(text, parent): """ Creates outlineItem from multimarkdown file. @param text: content of the file @param parent: appends item to parent (outlineItem) @return: outlineItem """ item = outlineItem(parent=parent) md, body = parseMMDFile(text, asDict=True) # Store metadata for k in md: if k in Outline.__members__: item.setData(Outline.__members__[k].value, str(md[k])) # Store body item.setData(Outline.text.value, str(body)) # Set file format to "md" # (Old version of manuskript had different file formats: text, t2t, html and md) # If file format is html, convert to plain text: if item.type() == "html": item.setData(Outline.text.value, HTML2PlainText(body)) if item.type() in ["txt", "t2t", "html"]: item.setData(Outline.type.value, "md") return item
def saveContent(content, parent): if content.strip(): child = outlineItem(title=parent.title(), parent=parent, _type="md") child._data[Outline.text] = content items.append(child) return ""
def addItem(self, _type="folder"): if len(self.selectedIndexes()) == 0: parent = self.rootIndex() else: parent = self.currentIndex() if _type == "text": _type = settings.defaultTextType item = outlineItem(title=qApp.translate("outlineBasics", "New"), _type=_type) self.model().appendItem(item, parent)
def addItem(self, _type="folder"): if len(self.selectedIndexes()) == 0: parent = self.rootIndex() else: parent = self.currentIndex() if _type == "text": _type = settings.defaultTextType item = outlineItem(title=qApp.translate("outlineBasics", "New"), _type=_type) self.model().appendItem(item, parent)
def addFile(f): fName, fExt = os.path.splitext(f) if fExt.lower() in ext: try: with open(os.path.join(dirpath, f), "r") as fr: content = fr.read() child = outlineItem(title=fName, _type="md", parent=item) child._data[Outline.text] = content items.append(child) except UnicodeDecodeError: # Probably not a text file pass
def addElement(parent, datas): if len(datas) == 2 and datas[1][1] == None or \ len(datas) == 1: # Next item is word count n = 0 for i in range(datas[0][0]): n += 1 item = outlineItem(title="{} {}".format( datas[0][1], str(n)), _type=_type, parent=parent) if len(datas) == 2: item.setData(Outline.setGoal.value, datas[1][0]) # parent.appendChild(item) else: n = 0 for i in range(datas[0][0]): n += 1 item = outlineItem(title="{} {}".format( datas[0][1], str(n)), _type="folder", parent=parent) # parent.appendChild(item) addElement(item, datas[1:])
def startImport(self, outlineModel): """ Where most of the magic happens. Is used by preview and by doImport (actual import). `outlineModel` is the model where the imported items are added. FIXME: Optimisation: when adding many outlineItems, outlineItem.updateWordCount is a bottleneck. It gets called a crazy number of time, and its not necessary. """ items = [] # We find the current selected format F = self._format # Parent item ID = self.settingsWidget.importUnderID() parentItem = outlineModel.getItemByID(ID) # Import in top-level folder? if self.settingsWidget.importInTopLevelFolder(): parent = outlineItem(title=os.path.basename(self.fileName), parent=parentItem) parentItem = parent items.append(parent) # Calling the importer rItems = F.startImport(self.fileName, parentItem, self.settingsWidget) items.extend(rItems) # Do transformations items = self.doTransformations(items) return True
def parseItems(cls, underElement, parentItem=None): items = [] title = underElement.get('text') if title is not None: card = outlineItem(parent=parentItem, title=title) items.append(card) body = "" note = underElement.get('_note') if note is not None and not cls.isWhitespaceOnly(note): #body = cls.restoreNewLines(note) body = note children = underElement.findall('outline') if children is not None and len(children) > 0: for el in children: items.extend(cls.parseItems(el, card)) else: card.setData(Outline.type.value, 'md') card.setData(Outline.text.value, body) return items
def parseItems(self, underElement, parentItem=None): items = [] # Title title = underElement.get('TEXT', "").replace("\n", " ") if not title: title = qApp.translate("Import", "Untitled") item = outlineItem(parent=parentItem, title=title) items.append(item) # URL url = underElement.get('LINK', None) # Rich text content content = "" content = underElement.find("richcontent") if content is not None: # In Freemind, can be note or node # Note: it's a note # Node: it's the title of the node, in rich text content_type = content.get("TYPE", "NOTE") content = ET.tostring(content.find("html")) if content and content_type == "NODE": # Content is title # convert rich text title (in html) to plain text title = HTML2PlainText(content) #.replace("\n", " ").strip() # Count the number of lines lines = [l.strip() for l in title.split("\n") if l.strip()] # If there is one line, we use it as title. # Otherwise we leave it to be inserted as a note. if len(lines) == 1: item.setData(Outline.title.value, "".join(lines)) content = "" if content: # Set the note content as text value content = HTML2MD(content) item.setData(Outline.notes.value, content) if url: # Set the url in notes item.setData(Outline.notes.value, item.data(Outline.notes.value) + "\n\n" + url) children = underElement.findall('node') # Process children if children is not None and len(children) > 0: for c in children: items.extend(self.parseItems(c, item)) # Process if no children elif self.getSetting("importTipAs").value() == "Text": # Transform item to text item.setData(Outline.type.value, 'md') # Move notes to text if item.data(Outline.notes.value): item.setData(Outline.text.value, item.data(Outline.notes.value)) item.setData(Outline.notes.value, "") return items
def addFolder(d): child = outlineItem(title=d, parent=item) items.append(child) stack[os.path.join(dirpath, d)] = child
def addTitle(name, parent, level): child = outlineItem(title=name, parent=parent) child.__miLevel = level items.append(child) return child