sphinxcontrib-docx 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,43 @@
1
+ from sphinx.util.osutil import make_filename
2
+ from sphinxcontrib.docx.builder import DocxBuilder
3
+
4
+
5
+ def setup(app):
6
+ app.add_builder(DocxBuilder)
7
+
8
+ def default_docx_documents(conf):
9
+ start_doc = conf.master_doc
10
+ filename = '%s.docx' % make_filename(conf.project)
11
+ title = conf.project
12
+ # author configuration value is available from Sphinx 1.8
13
+ author = getattr(conf, 'author', 'sphinxcontrib-docx')
14
+ properties = {
15
+ 'title': title,
16
+ 'creator': author,
17
+ 'subject': '',
18
+ 'category': '',
19
+ 'description': 'This document was generated by sphinxcontrib-docx',
20
+ 'keywords': ['python', 'Office Open XML', 'Word'],
21
+ }
22
+ toc_only = False
23
+ return [(start_doc, filename, properties, toc_only)]
24
+
25
+ app.add_config_value('docx_documents', default_docx_documents, 'env')
26
+ app.add_config_value('docx_style', '', 'env')
27
+ app.add_config_value('docx_pagebreak_before_section', 0, 'env')
28
+ app.add_config_value('docx_pagebreak_before_file', 0, 'env')
29
+ app.add_config_value('docx_pagebreak_before_table_of_contents', -1, 'env')
30
+ app.add_config_value('docx_pagebreak_after_table_of_contents', 0, 'env')
31
+ app.add_config_value('docx_coverpage', True, 'env')
32
+ app.add_config_value('docx_update_fields', False, 'env')
33
+ app.add_config_value('docx_hide_section_numbers', False, 'env')
34
+ app.add_config_value('docx_hide_subtoctrees', False, 'env')
35
+ app.add_config_value('docx_table_caption_position', 'above', 'env')
36
+ app.add_config_value('docx_table_options', {
37
+ 'landscape_columns': 0,
38
+ 'in_single_page': False,
39
+ 'row_splittable': True,
40
+ 'header_in_all_page': False,
41
+ }, 'env')
42
+ app.add_config_value('docx_style_names', {}, 'env')
43
+ app.add_config_value('docx_nested_character_style', True, 'env')
@@ -0,0 +1,147 @@
1
+ # -*- coding: utf-8 -*-
2
+ """
3
+ sphinxcontrib-docx
4
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~
5
+
6
+ OpenXML Document Sphinx builder.
7
+
8
+ :copyright:
9
+ Copyright 2010 by shimizukawa at gmail dot com (Sphinx-users.jp).
10
+ :license: BSD, see LICENSE for details.
11
+ """
12
+
13
+ import os
14
+
15
+ from docutils import nodes
16
+ from docutils.io import BinaryFileOutput
17
+ from sphinx import addnodes
18
+ from sphinx.builders import Builder
19
+ from sphinx.errors import ConfigError
20
+ from sphinx.util import logging
21
+ from sphinx.util.docutils import new_document
22
+ from sphinx.util.osutil import ensuredir
23
+
24
+ from sphinxcontrib.docx.writer import DocxWriter, DocxTranslator
25
+
26
+
27
+ class DocxBuilder(Builder):
28
+ # pylint: disable=attribute-defined-outside-init
29
+ name = 'docx'
30
+ format = 'docx'
31
+ out_suffix = '.docx'
32
+ default_translator_class = DocxTranslator
33
+
34
+ def init(self):
35
+ if self.config.docx_table_caption_position not in ('above', 'below'):
36
+ raise ConfigError("docx_table_caption_position must be 'above' or 'below'")
37
+ self.imagedir = '_images'
38
+ self._logger = logging.getLogger('sphinxcontrib.docx')
39
+ self._docx_documents = []
40
+
41
+ def get_outdated_docs(self):
42
+ return 'pass'
43
+
44
+ def get_target_uri(self, docname, typ=None):
45
+ return docname
46
+
47
+ def prepare_writing(self, docnames):
48
+ for entry in self.config.docx_documents:
49
+ if entry[0] not in self.env.all_docs:
50
+ self._logger.warning(
51
+ 'unknown document %s is found '
52
+ 'in docx_documents' % entry[0])
53
+ continue
54
+ if not entry[1]:
55
+ self._logger.warning(
56
+ 'invalid filename %s is found for %s '
57
+ 'in docx_documents' % (entry[1], entry[0]))
58
+ continue
59
+ self._docx_documents.append(entry)
60
+ if not self._docx_documents:
61
+ self._logger.warning('no valid entry is found in docx_documents')
62
+ self.writer = DocxWriter(self)
63
+
64
+ def assemble_doctree(self, master, toctree_only):
65
+ tree = self.env.get_doctree(master)
66
+ if toctree_only:
67
+ doc = new_document('sphinxcontrib/docx/builder.py')
68
+ for toctree in tree.traverse(addnodes.toctree):
69
+ # ids is not assigned to toctree, but to the parent
70
+ toctree.get('ids').extend(toctree.parent.get('ids'))
71
+ doc.append(toctree)
72
+ tree = doc
73
+ tree = insert_all_toctrees(tree, master, self.env, [])
74
+ tree['docname'] = master
75
+ self._logger.info('')
76
+ # TODO: Support cross references
77
+ return tree
78
+
79
+ def make_numfig_map(self):
80
+ numfig_map = {}
81
+ for docname, item in self.env.toc_fignumbers.items():
82
+ for figtype, info in item.items():
83
+ prefix = self.config.numfig_format.get(figtype)
84
+ if prefix is None:
85
+ continue
86
+ _, num_map = numfig_map.setdefault(figtype, (prefix, {}))
87
+ for node_id, num in info.items():
88
+ key = '%s/%s' % (docname, node_id)
89
+ num_map[key] = num
90
+ return numfig_map
91
+
92
+ def make_numsec_map(self):
93
+ numsec_map = {}
94
+ for docname, info in self.env.toc_secnumbers.items():
95
+ for node_id, num in info.items():
96
+ key = '%s/%s' % (docname, node_id)
97
+ numsec_map[key] = num
98
+ return numsec_map
99
+
100
+ def write(self, build_docnames, updated_docnames, method='update'):
101
+ docnames = set(self.env.all_docs)
102
+
103
+ self._logger.info('preparing documents... ', nonl=True)
104
+ self.prepare_writing(docnames)
105
+ self._logger.info('done')
106
+
107
+ for entry in self._docx_documents:
108
+ start_doc, docname, props = entry[:3]
109
+ toctree_only = entry[3] if len(entry) > 3 else False
110
+
111
+ self._logger.info('processing %s... ' % docname, nonl=True)
112
+ doctree = self.assemble_doctree(start_doc, toctree_only)
113
+ self.doc_properties = props
114
+ self._logger.info('writing... ', nonl=True)
115
+ self.write_doc(docname, doctree)
116
+ self._logger.info('done')
117
+
118
+ def write_doc(self, docname, doctree):
119
+ outfilename = os.path.join(self.outdir, docname)
120
+ ensuredir(os.path.dirname(outfilename))
121
+ destination = BinaryFileOutput(destination_path=outfilename)
122
+ self.writer.write(doctree, destination)
123
+
124
+ def finish(self):
125
+ pass
126
+
127
+ def insert_all_toctrees(tree, docname, env, traversed):
128
+ tree = tree.deepcopy()
129
+ env.apply_post_transforms(tree, docname)
130
+ for toctreenode in tree.traverse(addnodes.toctree):
131
+ nodeid = 'docx_expanded_toctree%d' % id(toctreenode)
132
+ newnodes = nodes.container(ids=[nodeid])
133
+ toctreenode['docx_expanded_toctree_refid'] = nodeid
134
+ includefiles = toctreenode['includefiles']
135
+ for includefile in includefiles:
136
+ if includefile in traversed:
137
+ continue
138
+ traversed.append(includefile)
139
+ subtree = insert_all_toctrees(
140
+ env.get_doctree(includefile), includefile, env, traversed)
141
+ start_of_file = addnodes.start_of_file(docname=includefile)
142
+ start_of_file.children = subtree.children
143
+ newnodes.append(start_of_file)
144
+ parent = toctreenode.parent
145
+ index = parent.index(toctreenode)
146
+ parent.insert(index + 1, newnodes)
147
+ return tree
@@ -0,0 +1 @@
1
+ from sphinxcontrib.docx.docx.docx import *