File: export.py

package info (click to toggle)
zim 0.76.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 10,952 kB
  • sloc: python: 68,612; xml: 1,270; javascript: 512; sh: 101; makefile: 47
file content (775 lines) | stat: -rw-r--r-- 22,980 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775

# Copyright 2009-2014 Jaap Karssenberg <jaap.karssenberg@gmail.com>



import tests

import os

from gi.repository import Gtk

from functools import partial

from zim.config import data_file, SectionedConfigDict
from zim.notebook import Path, Page, Notebook, init_notebook, \
	interwiki_link, get_notebook_list, NotebookInfo
from zim.templates import list_templates

from zim.main import ExportCommand, UsageError

from zim.export import *
from zim.export.layouts import *
from zim.export.linker import *
from zim.export.selections import *
from zim.export.template import *
from zim.export.exporters.files import *
from zim.export.exporters.mhtml import MHTMLExporter

from zim.templates import Template
from zim.templates.expression import ExpressionParameter, \
	ExpressionFunctionCall, ExpressionList

from zim.notebook import Path

from zim.fs import adapt_from_oldfs
from zim.newfs import File, Folder, FilePath


class TestMultiFileLayout(tests.TestCase):

	#  dir/
	#   `--> _resources/
	#   `--> page.html
	#   `--> page/
	#         `--> attachment.png

	def runTest(self):
		dir = self.setUpFolder()
		rdir = dir.folder('_resources')


		layout = MultiFileLayout(dir, 'html')
		self.assertEqual(layout.relative_root, dir)
		self.assertEqual(layout.resources_dir(), rdir)

		for path, file, adir in (
			(Path('Foo'), dir.file('Foo.html'), dir.folder('Foo')),
			(Path('Foo:Bar'), dir.file('Foo/Bar.html'), dir.folder('Foo/Bar')),
		):
			self.assertEqual(layout.page_file(path), file)
			self.assertEqual(layout.attachments_dir(path), adir)

		self.assertRaises(ValueError, layout.page_file, Path(':'))


		layout = MultiFileLayout(dir, 'html', namespace=Path('Test'))
		self.assertEqual(layout.relative_root, dir)
		self.assertEqual(layout.resources_dir(), rdir)

		for path, file, adir in (
			(Path('Test:Foo'), dir.file('Foo.html'), dir.folder('Foo')),
			(Path('Test:Foo:Bar'), dir.file('Foo/Bar.html'), dir.folder('Foo/Bar')),
		):
			self.assertEqual(layout.page_file(path), file)
			self.assertEqual(layout.attachments_dir(path), adir)

		self.assertRaises(ValueError, layout.page_file, Path(':'))
		self.assertRaises(ValueError, layout.page_file, Path('Foo'))


class TestFileLayout(tests.TestCase):

	#  page.html
	#  page_files/
	#   `--> attachment.png
	#   `--> subpage.html
	#   `--> subpage/attachment.pdf
	#   `--> _resources/

	def runTest(self):
		tdir = self.setUpFolder()
		topfile = tdir.file('page.html')
		dir = tdir.folder('page_files')
		rdir = dir.folder('_resources')

		layout = FileLayout(topfile, Path('Test'), 'html')
		self.assertEqual(layout.relative_root, dir.parent())
		self.assertEqual(layout.resources_dir(), rdir)

		for path, file, adir in (
			(Path('Test'), topfile, dir),
			(Path('Test:Foo'), dir.file('Foo.html'), dir.folder('Foo')),
			(Path('Test:Foo:Bar'), dir.file('Foo/Bar.html'), dir.folder('Foo/Bar')),
		):
			self.assertEqual(layout.page_file(path), file)
			self.assertEqual(layout.attachments_dir(path), adir)

		self.assertRaises(ValueError, layout.page_file, Path(':'))
		self.assertRaises(ValueError, layout.page_file, Path('Foo'))


class TestSingleFileLayout(tests.TestCase):

	# page.html
	#  page_files/
	#   `--> attachment.png
	#   `--> subpage/attachment.pdf
	#   `--> _resources/

	def runTest(self):
		tdir = self.setUpFolder()
		topfile = tdir.file('page.html')
		dir = tdir.folder('page_files')
		rdir = dir.folder('_resources')

		layout = SingleFileLayout(topfile, page=Path('Test'))
		self.assertEqual(layout.relative_root, dir.parent())
		self.assertEqual(layout.resources_dir(), rdir)

		for path, file, adir in (
			(Path('Test'), topfile, dir),
			(Path('Test:Foo'), topfile, dir.folder('Foo')),
			(Path('Test:Foo:Bar'), topfile, dir.folder('Foo/Bar')),
		):
			self.assertEqual(layout.page_file(path), file)
			self.assertEqual(layout.attachments_dir(path), adir)

		self.assertRaises(ValueError, layout.page_file, Path(':'))
		self.assertRaises(ValueError, layout.page_file, Path('Foo'))



class TestLinker(tests.TestCase):

	def runTest(self):
		notebook = self.setUpNotebook(content=('foo', 'bar', 'foo:bar',))
		folder = self.setUpFolder('layout')

		layout = MultiFileLayout(folder, 'html')
		source = Path('foo:bar')
		output = layout.page_file(source)

		linker = ExportLinker(notebook, layout,
			source=source, output=output, usebase=True
		)

		self.assertEqual(linker.link('+dus'), './bar/dus.html')
		self.assertEqual(linker.link('dus'), './dus.html')
		self.assertEqual(linker.link('#heading'), '#heading')
		self.assertEqual(linker.link('dus#heading'), './dus.html#heading')
		self.assertEqual(linker.link('./dus.pdf'), './bar/dus.pdf')
		self.assertEqual(linker.link('../dus.pdf'), './dus.pdf')
		self.assertEqual(linker.link('../../dus.pdf'), '../dus.pdf')

		extpath = 'C:\\dus.pdf' if os.name == 'nt' else '/dus.pdf'
		self.assertEqual(linker.link(extpath), FilePath(extpath).uri)

		# TODO:
		# 	img
		# 	icon
		# 	resource
		# 	resolve_source_file
		# 	page_object
		# 	file_object
		#
		#	document_root_url

		## setup environment for interwiki link
		if os.name == 'nt':
			uri = 'file:///C:/foo'
		else:
			uri = 'file:///foo'

		list = get_notebook_list()
		list.append(NotebookInfo(uri, interwiki='foo'))
		list.write()
		##

		href = interwiki_link('foo?Ideas:Task List')
		self.assertIsNotNone(href)
		self.assertEqual(linker.link('foo?Ideas:Task List'), uri + '/Ideas/Task_List.txt')





class TestExportTemplateContext(tests.TestCase):

	def setUp(self):
		notebook = self.setUpNotebook(content=tests.FULL_NOTEBOOK)
		output_folder = self.setUpFolder()
		layout = MultiFileLayout(output_folder, 'html')
		linker_factory = partial(ExportLinker,
			notebook=notebook,
			layout=layout,
			output=layout.page_file(Path('test')),
			usebase=True
		)
		dumper_factory = get_format('html').Dumper

		title = 'Test Export'
		self.content = [notebook.get_page(Path('Test:foo'))]
		self.context = ExportTemplateContext(
			notebook, linker_factory, dumper_factory,
			title, self.content, special=None,
			home=None, up=None, prevpage=None, nextpage=None,
			links=None,
		)

	def runTest(self):
		def get(name):
			param = ExpressionParameter(name)
			return param(self.context)

		# Test context setup
		self.assertIsInstance(get('generator.name'), str)
		self.assertTrue(get('generator.name').startswith('Zim'))
		self.assertIsInstance(get('generator.user'), str)

		self.assertEqual(get('title'), 'Test Export')

		pages = list(get('pages'))
		self.assertEqual(len(pages), 1)
		self.assertTrue(all(isinstance(o, PageProxy) for o in pages))
		self.assertEqual(pages[0]._page, self.content[0])
		## TODO
		# pages
		#   content
		#   special
		#
		# navigation	- links to other export pages (if not included here)
		#	home
		#	up
		# 	prev			-- prev export file or None
		# 	next			-- next export file or None
		#
		# links			-- links to other export pages (index & plugins / ...) - sorted dict to have Index, Home first followed by plugins
		#
		#	link
		#		.name
		#		.basename


		# Test PageProxy
		self.context['mypage'] = pages[0]
		self.assertEqual(get('mypage.title'), 'Foo')
		self.assertEqual(get('mypage.name'), 'Test:foo')
		self.assertEqual(get('mypage.namespace'), 'Test')
		self.assertEqual(get('mypage.basename'), 'foo')

		self.assertEqual(get('mypage.heading'), 'Foo')
		self.assertIsInstance(get('mypage.content'), str)
		self.assertIsInstance(get('mypage.body'), str)
		#TODO self.assertIsInstance(get('mypage.meta'), dict)


		#			.links
		#			.backlinks
		#			.attachments
		#


		# Test HeadingsProxy
		mycall = ExpressionFunctionCall(
			ExpressionParameter('mypage.headings'),
			ExpressionList(),
		)
		headings = list(mycall(self.context))
		self.assertEqual(len(headings), 2)

		self.context['h1'] = headings[0]
		self.context['h2'] = headings[1]
		self.assertEqual(get('h1.level'), 1)
		self.assertEqual(get('h2.level'), 2)
		self.assertIsInstance(get('h1.heading'), str)
		self.assertIsInstance(get('h1.body'), str)
		self.assertIsInstance(get('h1.content'), str)

		# Test FileProxy
		#				file
		#					.basename
		#					.mtime
		#					.size
		#

		## TODO
		# options		-- dict with template options (for format)
		#
		# toc([page])			-- iter of headings in this page or all of pages
		# index([namespace])	-- index of full export job, not just in this page
		# uri(link|file)
		# resource(file)
		# anchor(page|section)
		#
		# From template:
		# range() / len() / sorted() / reversed()
		# strftime()
		# strfcal()
		#
		# test single page by "IF loop.first and loop.last"


		## TODO test all of the attributes / items accesible through the
		##      context dict are string, expressionfunction, or proxy defined in this module

		## TODO test modification of options by template ends up in context
		##      test setting other local paramters in template does NOT affect context object
		##      test setting page meta is NOT allowed

		## TODO list simple template with processor to test looping through pages



class TestPageProxyWithFormattedHeading(tests.TestCase):

	CONTENT = {
		'test': '''\
======= This is a **heading** with __formatting__ =======

Some content here
'''
	}

	def runTest(self):
		from zim.formats import StubLinker
		notebook = self.setUpNotebook(content=self.CONTENT)
		page = notebook.get_page(Path('test'))
		dumper = get_format('html').Dumper()
		linker = StubLinker()
		proxy = PageProxy(notebook, page, dumper, linker)

		self.assertEqual(proxy.title, 'This is a heading with formatting')
		self.assertEqual(proxy.heading, 'This is a <b>heading</b> with <u>formatting</u>')
		self.assertIsInstance(proxy.body, str)
		self.assertTrue(proxy.body.startswith('<p>'))
		self.assertIsInstance(proxy.content, str)
		self.assertTrue(proxy.content.startswith('<h1>'))


class TestPageProxyWithEmptyPage(tests.TestCase):

	# Note: specifically test there are no exceptions here ...

	CONTENT = {
		'test': ''
	}

	def runTest(self):
		from zim.formats import StubLinker
		notebook = self.setUpNotebook(content=self.CONTENT)
		page = notebook.get_page(Path('test'))
		dumper = get_format('html').Dumper()
		linker = StubLinker()
		proxy = PageProxy(notebook, page, dumper, linker)

		self.assertEqual(proxy.title, 'test')
		self.assertEqual(proxy.heading, '')
		self.assertIsInstance(proxy.body, str)
		self.assertIsInstance(proxy.content, str)



class TestPageSelections(tests.TestCase):

	def _test_iface(self, selection):
		self.assertIsNotNone(selection.name)
		self.assertIsNotNone(selection.title)
		self.assertIsNotNone(selection.notebook)
		for p in selection:
			self.assertIsInstance(p, Page)

	# TODO add alternative method to walk names for ToC
	# TODO add __len__ that gives total pages for progress
	# TODO Use collections subclass to make interface complete ?

	def testAllPages(self):
		selection = AllPages(self.setUpNotebook(content=tests.FULL_NOTEBOOK))
		self._test_iface(selection)

	def testSinglePage(self):
		notebook = self.setUpNotebook(content=tests.FULL_NOTEBOOK)
		page = notebook.get_page(Path('Test'))
		selection = SinglePage(notebook, page)
		self._test_iface(selection)
		self.assertIsNotNone(selection.prefix)

	def testSubPages(self):
		notebook = self.setUpNotebook(content=tests.FULL_NOTEBOOK)
		page = notebook.get_page(Path('Test'))
		selection = SinglePage(notebook, page)
		self._test_iface(selection)
		self.assertIsNotNone(selection.prefix)


class TestMultiFileExporter(tests.TestCase):

	def runTest(self):
		folder = self.setUpFolder()
		notebook = self.setUpNotebook(content=tests.FULL_NOTEBOOK)
		pages = AllPages(notebook)

		exporter = build_notebook_exporter(folder, 'html', 'Default', index_page='Index')
		self.assertIsInstance(exporter, MultiFileExporter)
		exporter.export(pages)

		file = exporter.layout.page_file(Path('roundtrip'))
		text = file.read()
		self.assertIn('Lorem ipsum dolor sit amet', text)

		file = exporter.layout.page_file(Path('Index'))
		text = file.read()
		self.assertIn('<li><a href="./roundtrip.html" title="roundtrip" class="page">roundtrip</a></li>', text)


class TestSingleFileExporter(tests.TestCase):

	def runTest(self):
		folder = self.setUpFolder()
		file = folder.file('export.html')
		notebook = self.setUpNotebook(content=tests.FULL_NOTEBOOK)
		pages = AllPages(notebook)

		exporter = build_single_file_exporter(file, 'html', 'Default')
		self.assertIsInstance(exporter, SingleFileExporter)
		exporter.export(pages)

		text = file.read()
		self.assertIn('Lorem ipsum dolor sit amet', text)


class TestMHTMLExporter(tests.TestCase):

	def runTest(self):
		dir = self.setUpFolder()
		file = dir.file('export.mht')
		notebook = self.setUpNotebook(content=tests.FULL_NOTEBOOK)
		pages = AllPages(notebook)

		exporter = build_mhtml_file_exporter(file, 'Default')
		self.assertIsInstance(exporter, MHTMLExporter)
		exporter.export(pages)

		text = file.read()
		self.assertIn('Lorem ipsum dolor sit amet', text)


class TestTemplateOptions(tests.TestCase):

	def runTest(self):
		dir = self.setUpFolder()
		file = dir.file('test.tex')
		page = Path('roundtrip')
		exporter = build_page_exporter(file, 'latex', 'Article', page)

		notebook = self.setUpNotebook(content=tests.FULL_NOTEBOOK)
		selection = SinglePage(notebook, page)

		with tests.LoggingFilter('zim.formats.latex', 'Could not find latex equation'):
			exporter.export(selection)
		result = file.read()
		#~ print result
		self.assertIn('\\section{Head1}', result) # this implies that document_type "article" was indeed used


class TestExportFormat(object):

	def runTest(self):
		output_folder = self.setUpFolder()
		notebook = self.setUpNotebook(content=tests.FULL_NOTEBOOK)

		i = 0
		for template, file in list_templates(self.format):
			#print('Testing template: %s' % template)
			pages = AllPages(notebook) # TODO - sub-section ?
			exporter = build_notebook_exporter(output_folder.folder(template), self.format, template)
			self.assertIsInstance(exporter, MultiFileExporter)

			with tests.LoggingFilter('zim.formats.latex', 'Could not find latex equation'):
				exporter.export(pages)

			file = exporter.layout.page_file(Path('roundtrip'))
			text = file.read()
			self.assertIn('Lorem ipsum dolor sit amet', text)

			i += 1

		if self.format in ('html', 'latex'):
			self.assertTrue(i >= 3) # Ensure we actually tested something ..


class TestExportFormatHtml(TestExportFormat, tests.TestCase):
	format = 'html'


class TestExportFormatLatex(TestExportFormat, tests.TestCase):
	format = 'latex'


class TestExportFormatMarkDown(TestExportFormat, tests.TestCase):
	format = 'markdown'


class TestExportFormatRst(TestExportFormat, tests.TestCase):
	format = 'rst'


class TestExportCommand(tests.TestCase):

	def setUp(self):
		self.tmpfolder = self.setUpFolder(mock=tests.MOCK_ALWAYS_REAL)
		self.notebookfolder = self.tmpfolder.folder('notebook')
		init_notebook(self.notebookfolder)

	def testOptions(self):
		# Only testing we get a valid exporter, not the full command,
		# because the command is very slow

		## Full notebook, minimal options
		cmd = ExportCommand('export')
		cmd.parse_options(self.notebookfolder.path)
		self.assertRaises(UsageError, cmd.get_exporter, None)

		cmd = ExportCommand('export')
		cmd.parse_options(self.notebookfolder.path,
			'--output', self.tmpfolder.folder('output').path,
		)
		exp = cmd.get_exporter(None)
		self.assertIsInstance(exp, MultiFileExporter)
		self.assertIsInstance(exp.layout, MultiFileLayout)
		self.assertIsInstance(exp.layout.dir, Folder)
		self.assertIsInstance(exp.template, Template)
		self.assertIsNone(exp.document_root_url)
		self.assertIsNotNone(exp.format)
		self.assertIsNone(exp.index_page)

		## Full notebook, full options
		cmd = ExportCommand('export')
		cmd.parse_options(self.notebookfolder.path,
			'--format', 'markdown',
			'--template', './tests/data/TestTemplate.html',
			'--output', self.tmpfolder.folder('output').path,
			'--root-url', '/foo/',
			'--index-page', 'myindex',
			'--overwrite',
		)
		exp = cmd.get_exporter(None)
		self.assertIsInstance(exp, MultiFileExporter)
		self.assertIsInstance(exp.layout, MultiFileLayout)
		self.assertIsInstance(exp.layout.dir, Folder)
		self.assertIsInstance(exp.template, Template)
		self.assertIsNotNone(exp.document_root_url)
		self.assertIsNotNone(exp.format)
		self.assertIsNotNone(exp.index_page)


		## Full notebook, single page
		cmd = ExportCommand('export')
		cmd.parse_options(self.notebookfolder.path,
			'--format', 'markdown',
			'--template', './tests/data/TestTemplate.html',
			'--output', self.tmpfolder.file('output.md').path,
			'-s'
		)
		exp = cmd.get_exporter(None)
		self.assertIsInstance(exp, SingleFileExporter)
		self.assertIsInstance(exp.layout, SingleFileLayout)
		self.assertIsInstance(exp.layout.file, File)

		## Single page
		cmd = ExportCommand('export')
		cmd.parse_options(self.notebookfolder.path, 'Foo:Bar',
			'--output', self.tmpfolder.folder('output').path,
		)
		exp = cmd.get_exporter(Path('Foo:Bar'))
		self.assertIsInstance(exp, MultiFileExporter)
		self.assertIsInstance(exp.layout, FileLayout)
		self.assertIsInstance(exp.layout.file, File)
		self.assertIsInstance(exp.template, Template)
		self.assertIsNone(exp.document_root_url)
		self.assertIsNotNone(exp.format)
		self.assertIsNone(exp.index_page)

		cmd = ExportCommand('export')
		cmd.parse_options(self.notebookfolder.path, 'Foo:Bar',
			'--recursive',
			'--output', self.tmpfolder.folder('output').path,
		)
		exp = cmd.get_exporter(Path('Foo:Bar'))
		self.assertIsInstance(exp, MultiFileExporter)
		self.assertIsInstance(exp.layout, FileLayout)
		self.assertIsInstance(exp.layout.file, File)
		self.assertIsInstance(exp.template, Template)
		self.assertIsNone(exp.document_root_url)
		self.assertIsNotNone(exp.format)
		self.assertIsNone(exp.index_page)

		cmd = ExportCommand('export')
		cmd.parse_options(self.notebookfolder.path, 'Foo:Bar',
			'-rs',
			'--output', self.tmpfolder.folder('output').path,
		)
		exp = cmd.get_exporter(Path('Foo:Bar'))
		self.assertIsInstance(exp, SingleFileExporter)
		self.assertIsInstance(exp.layout, SingleFileLayout)
		self.assertIsInstance(exp.layout.file, File)
		self.assertIsInstance(exp.template, Template)
		self.assertIsNone(exp.document_root_url)
		self.assertIsNotNone(exp.format)

		## MHTML exporter
		cmd = ExportCommand('export')
		cmd.parse_options(self.notebookfolder.path, 'Foo:Bar',
			'-rs', '--format', 'mhtml',
			'--output', self.tmpfolder.folder('output').path,
		)
		exp = cmd.get_exporter(Path('Foo:Bar'))
		self.assertIsInstance(exp, MHTMLExporter)
		self.assertIsInstance(exp.file, File)
		self.assertIsInstance(exp.template, Template)
		self.assertIsNone(exp.document_root_url)

	def testExport(self):
		# Only test single page, just to show "run()" works
		file = self.notebookfolder.file('Foo/Bar.txt')
		file.write(
			'Content-Type: text/x-zim-wiki\n\n'
			'=== Foo\ntest 123\n'
		)

		output = self.tmpfolder.file('output.html')

		cmd = ExportCommand('export')
		cmd.parse_options(self.notebookfolder.path, 'Foo:Bar',
			'--output', output.path,
			'--template', 'tests/data/TestTemplate.html'
		)
		cmd.run()

		self.assertTrue(output.exists())
		html = output.read()
		self.assertTrue('<h1>Foo' in html)
		self.assertTrue('test 123' in html)


class TestExportDialog(tests.TestCase):

	def testDialog(self):
		'''Test ExportDialog'''
		from zim.gui.exportdialog import ExportDialog, ExportDoneDialog

		dir = self.setUpFolder(mock=tests.MOCK_ALWAYS_REAL)
		notebook = self.setUpNotebook(content={'foo': 'test 123\n', 'bar': 'test 123\n'})

		window = Gtk.Window()
		window.notebook = notebook

		## Test export all pages
		dialog = ExportDialog(window, notebook, Path('foo'))
		dialog.set_page(0)

		page = dialog.get_page()
		page.form['selection'] = 'all'
		dialog.next_page()

		page = dialog.get_page()
		page.form['format'] = 'HTML'
		page.form['template'] = 'Print'
		dialog.next_page()

		page = dialog.get_page()
		page.form['folder'] = dir
		page.form['index'] = 'INDEX_PAGE'
		with tests.DialogContext(ExportDoneDialog):
			dialog.assert_response_ok()

		file = dir.file('foo.html')
		self.assertTrue(file.exists())
		text = file.read()
		self.assertTrue('<!-- Wiki content -->' in text, 'template used')

		#~ print dialog.uistate
		self.assertEqual(dialog.uistate, window.notebook.state['ExportDialog'])
		self.assertIsInstance(adapt_from_oldfs(dialog.uistate['output_folder']), Folder)

		## Test export single page
		dialog = ExportDialog(window, notebook, Path('foo'))
		dialog.set_page(0)

		page = dialog.get_page()
		page.form['selection'] = 'page'
		page.form['page'] = 'foo'
		dialog.next_page()

		page = dialog.get_page()
		page.form['format'] = 'HTML'
		page.form['template'] = 'Print'
		dialog.next_page()

		page = dialog.get_page()
		page.form['file'] = dir.file('SINGLE_FILE_EXPORT.html').path
		with tests.DialogContext(ExportDoneDialog):
			dialog.assert_response_ok()

		file = dir.file('SINGLE_FILE_EXPORT.html')
		self.assertTrue(file.exists())
		text = file.read()
		self.assertTrue('<!-- Wiki content -->' in text, 'template used')

		#~ print dialog.uistate
		self.assertEqual(dialog.uistate, window.notebook.state['ExportDialog'])
		self.assertIsInstance(adapt_from_oldfs(dialog.uistate['output_file']), File)
		self.assertIsInstance(adapt_from_oldfs(dialog.uistate['output_folder']), Folder) # Keep this in state as well

	def testLogging(self):
		from zim.gui.exportdialog import LogContext

		mylogger = logging.getLogger('zim.export')
		foologger = logging.getLogger('zim.foo')
		log_context = LogContext()

		with tests.LoggingFilter(logger='zim', message='Test'):
			with log_context:
				mylogger.warning('Test export warning')
				mylogger.debug('Test export debug')
				foologger.warning('Test foo')

		file = log_context.file
		self.assertTrue(file.exists())
		#~ print(">>>\n", file.read(), "\n<<<")
		self.assertTrue('Test export warning' in file.read())
		self.assertFalse('Test export debug' in file.read())
		self.assertFalse('Test foo' in file.read())


class TestOverwrite(tests.TestCase):

	def testSingleFile(self):
		# TODO: run this with mock file
		# TODO: ensure template has resources
		# TODO: add attachements to test notebook

		folder = self.setUpFolder(mock=tests.MOCK_ALWAYS_REAL)
		file = folder.file('test.html')
		exporter = build_single_file_exporter(file, 'html', 'Default.html')

		notebook = self.setUpNotebook(content={'foo': 'test 123\n', 'bar': 'test 123\n'})
		pages = AllPages(notebook)

		# Now do it twice - should not raise for file exists
		exporter.export(pages)
		exporter.export(pages)

	def testMultiFile(self):
		# TODO: ensure template has resources
		# TODO: add attachements to test notebook

		folder = self.setUpFolder(mock=tests.MOCK_ALWAYS_REAL)
		exporter = build_notebook_exporter(folder, 'html', 'Default.html')

		notebook = self.setUpNotebook(content={'foo': 'test 123\n', 'bar': 'test 123\n'})
		pages = AllPages(notebook)

		# Now do it twice - should not raise for file exists
		exporter.export(pages)
		exporter.export(pages)