Zim

Merge lp:~hsoft/zim/dev into lp:~jaap.karssenberg/zim/pyzim

Proposed by Virgil Dupras
Status: Merged
Merged at revision: 611
Proposed branch: lp:~hsoft/zim/dev
Merge into: lp:~jaap.karssenberg/zim/pyzim
Diff against target: 184 lines (+69/-11)
6 files modified
tests/formats.py (+6/-0)
tests/gui.py (+18/-3)
tests/notebook.py (+12/-1)
zim/formats/__init__.py (+19/-6)
zim/gui/__init__.py (+2/-1)
zim/notebook.py (+12/-0)
To merge this branch: bzr merge lp:~hsoft/zim/dev
Reviewer Review Type Date Requested Status
Jaap Karssenberg Pending
Review via email: mp+133931@code.launchpad.net
To post a comment you must log in.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'tests/formats.py'
2--- tests/formats.py 2012-07-21 10:14:36 +0000
3+++ tests/formats.py 2012-11-12 14:30:51 +0000
4@@ -203,6 +203,12 @@
5 text = tree.tostring()
6 self.assertEqual(text, wanted)
7
8+ def testGetHeading(self):
9+ '''Test that ParseTree.get_heading() returns the first header's text.
10+ '''
11+ tree = ParseTree().fromstring(self.xml)
12+ self.assertEqual(tree.get_heading(), "Head 1")
13+
14 def testSetHeading(self):
15 '''Test ParseTree.set_heading()'''
16 tree = ParseTree().fromstring(self.xml)
17
18=== modified file 'tests/gui.py'
19--- tests/gui.py 2012-08-25 16:05:50 +0000
20+++ tests/gui.py 2012-11-12 14:30:51 +0000
21@@ -10,7 +10,8 @@
22 import gtk
23
24 from zim.errors import Error
25-from zim.notebook import get_notebook_list, Path, NotebookInfo
26+from zim.notebook import get_notebook_list, Path, Page, NotebookInfo
27+from zim.formats import ParseTree
28 from zim.fs import File, Dir
29 from zim.config import config_file
30 from zim.gui.clipboard import Clipboard
31@@ -163,12 +164,12 @@
32 dialog = zim.gui.RenamePageDialog(self.ui, path=Path('Test:foo:bar'))
33 self.assertTrue(dialog.form['update'])
34 self.assertTrue(dialog.form.widgets['update'].get_property('sensitive'))
35- self.assertTrue(dialog.form['head'])
36+ self.assertFalse(dialog.form['head']) # There is no heading
37 self.assertTrue(dialog.form.widgets['head'].get_property('sensitive'))
38 dialog.form['name'] = 'New'
39 dialog.assert_response_ok()
40 self.assertEqual(self.ui.mock_calls[-1],
41- ('do_rename_page', Path('Test:foo:bar'), 'New', True, True))
42+ ('do_rename_page', Path('Test:foo:bar'), 'New', False, True))
43
44 dialog = zim.gui.RenamePageDialog(self.ui, path=Path('New:bar'))
45 self.assertFalse(dialog.form['update'])
46@@ -180,6 +181,20 @@
47 self.assertEqual(self.ui.mock_calls[-1],
48 ('do_rename_page', Path('New:bar'), 'New', False, False))
49
50+ def testRenamePageDialogWithHeadingChanges(self):
51+ '''Test RenamePageDialog's heading auto-change option depending on
52+ whether we have a changed heading or not.
53+ '''
54+ tree = ParseTree().fromstring('<zim-tree></zim-tree>')
55+ tree.set_heading("bar")
56+ self.ui.page = Page(Path("Test:foo:bar"), parsetree=tree)
57+ self.ui.notebook.get_page = lambda path: self.ui.page
58+ dialog = zim.gui.RenamePageDialog(self.ui, path=Path("Test:foo:bar"))
59+ self.assertTrue(dialog.form['head'])
60+ tree.set_heading("different")
61+ dialog = zim.gui.RenamePageDialog(self.ui, path=Path("Test:foo:bar"))
62+ self.assertFalse(dialog.form['head'])
63+
64 def testDeletePageDialog(self):
65 '''Test DeletePageDialog'''
66 # just check inputs are OK - skip output
67
68=== modified file 'tests/notebook.py'
69--- tests/notebook.py 2012-10-06 12:26:18 +0000
70+++ tests/notebook.py 2012-11-12 14:30:51 +0000
71@@ -639,7 +639,7 @@
72 def generator(self, name):
73 return self.notebook.get_page(Path(name))
74
75- def runTest(self):
76+ def testMain(self):
77 '''Test Page object'''
78 TestPath.runTest(self)
79
80@@ -674,6 +674,17 @@
81 self.assertFalse(tree.hascontent)
82 page.set_parsetree(tree)
83 self.assertFalse(page.hascontent)
84+
85+ def testShouldAutochangeHeading(self):
86+ page = Page(Path("Foo"))
87+ page.readonly = False
88+ tree = ParseTree().fromstring('<zim-tree></zim-tree>')
89+ tree.set_heading("Foo")
90+ page.set_parsetree(tree)
91+ self.assertTrue(page.should_autochange_heading())
92+ tree.set_heading("Bar")
93+ page.set_parsetree(tree)
94+ self.assertFalse(page.should_autochange_heading())
95
96
97 class TestIndexPage(tests.TestCase):
98
99=== modified file 'zim/formats/__init__.py'
100--- zim/formats/__init__.py 2012-09-22 18:22:22 +0000
101+++ zim/formats/__init__.py 2012-11-12 14:30:51 +0000
102@@ -281,19 +281,32 @@
103 '''Parsing from file is not implemented, use fromstring() instead'''
104 raise NotImplementedError
105
106+ def _get_heading_element(self, level=1):
107+ children = self.getroot().getchildren()
108+ if children:
109+ first = children[0]
110+ if first.tag == 'h' and first.attrib['level'] >= level:
111+ return first
112+ return None
113+
114+ def get_heading(self, level=1):
115+ heading_elem = self._get_heading_element(level)
116+ if heading_elem is not None:
117+ return heading_elem.text
118+ else:
119+ return ""
120+
121 def set_heading(self, text, level=1):
122 '''Set the first heading of the parse tree to 'text'. If the tree
123 already has a heading of the specified level or higher it will be
124 replaced. Otherwise the new heading will be prepended.
125 '''
126 root = self.getroot()
127- children = root.getchildren()
128 tail = "\n"
129- if children:
130- first = children[0]
131- if first.tag == 'h' and first.attrib['level'] >= level:
132- tail = first.tail # Keep trailing text
133- root.remove(first)
134+ heading_elem = self._get_heading_element(level)
135+ if heading_elem is not None:
136+ tail = heading_elem.tail
137+ root.remove(heading_elem)
138 heading = Element('h', {'level': level})
139 heading.text = text
140 heading.tail = tail
141
142=== modified file 'zim/gui/__init__.py'
143--- zim/gui/__init__.py 2012-09-29 21:21:38 +0000
144+++ zim/gui/__init__.py 2012-11-12 14:30:51 +0000
145@@ -3229,6 +3229,7 @@
146
147 page = self.ui.notebook.get_page(self.path)
148 existing = (page.hascontent or page.haschildren)
149+ should_autochange_heading = page.should_autochange_heading()
150
151 self.vbox.add(gtk.Label(_('Rename page "%s"') % self.path.name))
152 # T: label in 'rename page' dialog - %s is the page name
153@@ -3255,7 +3256,7 @@
154 # T: Option in the 'rename page' dialog
155 ], {
156 'name': self.path.basename,
157- 'head': existing,
158+ 'head': should_autochange_heading,
159 'update': True,
160 })
161
162
163=== modified file 'zim/notebook.py'
164--- zim/notebook.py 2012-10-06 12:26:18 +0000
165+++ zim/notebook.py 2012-11-12 14:30:51 +0000
166@@ -2505,6 +2505,18 @@
167 for tag, attrib in tags.iteritems():
168 yield tag, attrib
169
170+ def should_autochange_heading(self):
171+ '''Returns whether the page should have its heading auto-chaged on
172+ rename/move.
173+
174+ @returns: C{True} when the heading can be auto-changed.
175+ '''
176+ tree = self.get_parsetree()
177+ if tree:
178+ return tree.get_heading() == self.basename
179+ else:
180+ return False
181+
182
183 class IndexPage(Page):
184 '''Class implementing a special page for displaying a namespace index'''