diff --git a/example.py b/example.py index 571c754..68435cc 100644 --- a/example.py +++ b/example.py @@ -1,39 +1,39 @@ -#-*- coding: utf-8 -*- +# -*- coding: utf-8 -*- import xmind -from xmind.core import workbook,saver +from xmind.core import workbook, saver from xmind.core.topic import TopicElement -w = xmind.load("test.xmind") # load an existing file or create a new workbook if nothing is found +w = xmind.load("test.xmind") # load an existing file or create a new workbook if nothing is found -s1=w.getPrimarySheet() # get the first sheet -s1.setTitle("first sheet") # set its title -r1=s1.getRootTopic() # get the root topic of this sheet -r1.setTitle("we don't care of this sheet") # set its title +s1 = w.getPrimarySheet() # get the first sheet +s1.setTitle("first sheet") # set its title +r1 = s1.getRootTopic() # get the root topic of this sheet +r1.setTitle("we don't care of this sheet") # set its title -s2=w.createSheet() # create a new sheet +s2 = w.createSheet() # create a new sheet s2.setTitle("second sheet") -r2=s2.getRootTopic() +r2 = s2.getRootTopic() r2.setTitle("root node") +t1 = TopicElement() # create a new element +t1.setTopicHyperlink(s1.getID()) # set a link from this topic to the first sheet given by s1.getID() +t1.setTitle("redirection to the first sheet") # set its title -t1=TopicElement() # create a new element -t1.setTopicHyperlink(s1.getID()) # set a link from this topic to the first sheet given by s1.getID() -t1.setTitle("redirection to the first sheet") # set its title - -t2=TopicElement() +t2 = TopicElement() t2.setTitle("second node") -t2.setURLHyperlink("https://xmind.net") # set an hyperlink +t2.setLabels("first label") # set notes (F3 in XMind) +t2.setURLHyperlink("https://xmind.net") # set an hyperlink -t3=TopicElement() +t3 = TopicElement() t3.setTitle("third node") -t3.setPlainNotes("notes for this topic") # set notes (F4 in XMind) +t3.setPlainNotes("notes for this topic") # set notes (F4 in XMind) +t3.setLabels("second label") # set notes (F3 in XMind) t3.setTitle("topic with \n notes") -t4=TopicElement() -t4.setFileHyperlink("logo.jpeg") # set a file hyperlink +t4 = TopicElement() +t4.setFileHyperlink("logo.jpeg") # set a file hyperlink t4.setTitle("topic with a file") - # then the topics must be added to the root element r2.addSubTopic(t1) @@ -41,12 +41,12 @@ r2.addSubTopic(t3) r2.addSubTopic(t4) -topics=r2.getSubTopics() # to loop on the subTopics +topics = r2.getSubTopics() # to loop on the subTopics for topic in topics: topic.addMarker("yes") -w.addSheet(s2) # the second sheet is now added to the workbook -rel=s2.createRelationship(t1.getID(),t2.getID(),"test") # create a relationship -s2.addRelationship(rel) # and add to the sheet +w.addSheet(s2) # the second sheet is now added to the workbook +rel = s2.createRelationship(t1.getID(), t2.getID(), "test") # create a relationship +s2.addRelationship(rel) # and add to the sheet -xmind.save(w,"test2.xmind") # and we save \ No newline at end of file +xmind.save(w, "test2.xmind") # and we save diff --git a/test.xmind b/test.xmind new file mode 100644 index 0000000..cbfb1ff Binary files /dev/null and b/test.xmind differ diff --git a/xmind/core/const.py b/xmind/core/const.py index a9b27d1..ee20bca 100644 --- a/xmind/core/const.py +++ b/xmind/core/const.py @@ -38,6 +38,7 @@ TAG_POSITION = "position" TAG_CHILDREN = "children" TAG_NOTES = "notes" +TAG_LABELS = "labels" TAG_RELATIONSHIP = "relationship" TAG_RELATIONSHIPS = "relationships" TAG_MARKERREFS = "marker-refs" @@ -75,3 +76,4 @@ HTML_FORMAT_NOTE = "html" PLAIN_FORMAT_NOTE = "plain" +PLAIN_FORMAT_LABEL = "label" diff --git a/xmind/core/label.py b/xmind/core/label.py new file mode 100644 index 0000000..8cc614e --- /dev/null +++ b/xmind/core/label.py @@ -0,0 +1,76 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +""" + xmind.core.labels + ~~~~~~~~~~~~~~~~ + + :copyright: + :license: + +""" + +__author__ = "evgeny2900@gmail.com " + +from . import const + +from .mixin import TopicMixinElement + + +class LabelsElement(TopicMixinElement): + TAG_NAME = const.TAG_LABELS + + def __init__(self, node=None, ownerTopic=None): + super(LabelsElement, self).__init__(node, ownerTopic) + + def getContent(self, format=const.PLAIN_FORMAT_LABEL): + """ Get labels content + + :parma format: specified returned content format, plain text + by default. + """ + + content = self.getFirstChildNodeByTagName(format) + + if not content: + return + + content = Labels(node=content, ownerTopic=self.getOwnerTopic()) + + return content.getTextContent() + + +class _LabelContentElement(TopicMixinElement): + def __init__(self, node=None, ownerTopic=None): + super(_LabelContentElement, self).__init__(node, ownerTopic) + + def getFormat(self): + return self.getImplementation().tagName + + +class Labels(_LabelContentElement): + """ Plain text label + + :param content: utf8 plain text. + :param node: `xml.dom.Element` object` + :param ownerTopic: `xmind.core.topic.TopicElement` object + + """ + + TAG_NAME = const.PLAIN_FORMAT_LABEL + + def __init__(self, content=None, node=None, ownerTopic=None): + super(Labels, self).__init__(node, ownerTopic) + if content is not None: + self.setTextContent(content) + + def setContent(self, content): + self.setTextContent(content) + + +def main(): + pass + + +if __name__ == '__main__': + main() diff --git a/xmind/core/topic.py b/xmind/core/topic.py index 9b011cd..e91748d 100644 --- a/xmind/core/topic.py +++ b/xmind/core/topic.py @@ -18,6 +18,7 @@ from .title import TitleElement from .position import PositionElement from .notes import NotesElement, PlainNotes +from xmind.core.label import LabelsElement, Labels from .markerref import MarkerRefElement from .markerref import MarkerRefsElement from .markerref import MarkerId @@ -331,6 +332,17 @@ def getNotes(self): if notes is not None: return NotesElement(notes, self) + def getLabels(self): + """ + Return `NotesElement` object` and invoke + `NotesElement.getContent()` to get labels content. + """ + + labels = self.getFirstChildNodeByTagName(const.TAG_LABELS) + + if labels is not None: + return NotesElement(labels, self) + def _set_notes(self): notes = self.getNotes() @@ -340,6 +352,15 @@ def _set_notes(self): return notes + def _set_labels(self): + labels = self.getLabels() + + if labels is None: + labels = LabelsElement(ownerTopic=self) + self.appendChild(labels) + + return labels + def setPlainNotes(self, content): """ Set plain text notes to topic @@ -355,6 +376,20 @@ def setPlainNotes(self, content): notes.appendChild(new) + def setLabels(self, content): + """ + Set plain text label to topic + :param content: + """ + labels = self._set_labels() + new = Labels(content, None, self) + + old = labels.getFirstChildNodeByTagName(new.getFormat()) + if old is not None: + labels.getImplementation().removeChild(old) + + labels.appendChild(new) + class ChildrenElement(WorkbookMixinElement): TAG_NAME = const.TAG_CHILDREN