Skip to content

Commit dd107ed

Browse files
authored
Merge pull request #1055 from sphinx-contrib/introduce-page-id-events
Introduce publish page id events
2 parents ffd9387 + 81db3e8 commit dd107ed

File tree

5 files changed

+79
-1
lines changed

5 files changed

+79
-1
lines changed

doc/directives.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ Common
203203
204204
.. versionadded:: 1.3
205205
.. versionchanged:: 2.2 Added ``editor`` and ``full-width`` support.
206+
.. versionchanged:: 2.8 Added ``guid`` support.
206207

207208
The ``confluence_metadata`` directive allows a user to define metadata
208209
information to be added during a publish event. This directive supports the
@@ -237,6 +238,13 @@ Common
237238
238239
See also :lref:`confluence_full_width`.
239240
241+
.. rst:directive:option:: guid: value
242+
:type: string
243+
244+
Assign a user-managed GUID value for a page. This value can be
245+
shared in a subset of :doc:`events <events>` generated by this
246+
extension.
247+
240248
.. rst:directive:option:: labels: value
241249
:type: space separated strings
242250

doc/events.rst

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,29 @@ The following outlines additional `events`_ supported by this extension.
66
Generic events
77
--------------
88

9+
.. event:: confluence-publish-attachment (app, docname, key, aid, meta)
10+
11+
:param app: :class:`.Sphinx`
12+
:param docname: ``str`` of the document name
13+
:param key: ``str`` of the attachment key
14+
:param aid: ``int`` of the upload id of the published attachment
15+
:param meta: ``dict`` of additional metadata for this event
16+
17+
Emitted when this extension has completed the upload of an attachment.
18+
19+
.. versionadded:: 2.8
20+
21+
.. event:: confluence-publish-page (app, docname, pid, meta)
22+
23+
:param app: :class:`.Sphinx`
24+
:param docname: ``str`` of the document name
25+
:param pid: ``int`` of the upload id of the published page
26+
:param meta: ``dict`` of additional metadata for this event
27+
28+
Emitted when this extension has completed the upload of a document.
29+
30+
.. versionadded:: 2.8
31+
932
.. event:: confluence-publish-point (app, point_url)
1033

1134
:param app: :class:`.Sphinx`
@@ -16,6 +39,24 @@ Generic events
1639

1740
.. versionadded:: 2.6
1841

42+
Advanced events
43+
---------------
44+
45+
.. event:: confluence-publish-override-pageid (app, docname, meta)
46+
47+
:param app: :class:`.Sphinx`
48+
:param docname: ``str`` of the document name
49+
:param meta: ``dict`` of additional metadata for this event
50+
:returns: ``int | None`` of the new page identifier
51+
52+
Emitted when this extension is about to determine the page identifier
53+
to publish a document. A configuration and register this event and
54+
return a new identifier to use for a page. If ``None`` is returned,
55+
the extension will operate in the same manner as if no override was
56+
provided.
57+
58+
.. versionadded:: 2.8
59+
1960
.. references ------------------------------------------------------------------
2061
2162
.. _events: https://www.sphinx-doc.org/en/master/extdev/event_callbacks.html

sphinxcontrib/confluencebuilder/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ def setup(app):
6060
app.add_builder(ConfluenceBuilder)
6161
app.add_builder(ConfluenceReportBuilder)
6262
app.add_builder(SingleConfluenceBuilder)
63+
app.add_event('confluence-publish-attachment')
64+
app.add_event('confluence-publish-override-pageid')
65+
app.add_event('confluence-publish-page')
6366
app.add_event('confluence-publish-point')
6467
app.add_post_transform(ConfluenceHyperlinkCollector)
6568

sphinxcontrib/confluencebuilder/builder.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -502,7 +502,19 @@ def publish_doc(self, docname, output):
502502

503503
data = self._prepare_page_data(docname, output)
504504

505-
if conf.confluence_publish_root and is_root_doc:
505+
metadata = self.metadata.get(docname, {})
506+
docguid = metadata.get('guid')
507+
508+
forced_page_id = self.app.emit_firstresult(
509+
'confluence-publish-override-pageid', docname, {
510+
'guid': docguid,
511+
'title': title,
512+
})
513+
514+
if forced_page_id:
515+
uploaded_id = self.publisher.store_page_by_id(title,
516+
forced_page_id, data)
517+
elif conf.confluence_publish_root and is_root_doc:
506518
uploaded_id = self.publisher.store_page_by_id(title,
507519
conf.confluence_publish_root, data)
508520
else:
@@ -590,6 +602,12 @@ def publish_doc(self, docname, output):
590602
if uploaded_id in self.legacy_pages:
591603
self.legacy_pages.remove(uploaded_id)
592604

605+
if uploaded_id:
606+
self.app.emit('confluence-publish-page', docname, uploaded_id, {
607+
'guid': docguid,
608+
'title': title,
609+
})
610+
593611
def _prepare_page_data(self, docname, output):
594612
data = {
595613
'content': output,
@@ -664,6 +682,13 @@ def publish_asset(self, key, docname, output, type_, hash_):
664682
if attachment_id in legacy_asset_info:
665683
legacy_asset_info.pop(attachment_id, None)
666684

685+
if attachment_id:
686+
self.app.emit('confluence-publish-attachment',
687+
docname, key, attachment_id, {
688+
'hash': hash_,
689+
'type': type_,
690+
})
691+
667692
def publish_finalize(self):
668693
if self.root_doc_page_id:
669694
if self.config.confluence_root_homepage is True:

sphinxcontrib/confluencebuilder/directives.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ class ConfluenceMetadataDirective(Directive):
200200
option_spec = {
201201
'editor': lambda x: directives.choice(x, EDITORS),
202202
'full-width': lambda x: directives.choice(x, ('true', 'false')),
203+
'guid': directives.unchanged,
203204
'labels': string_list,
204205
}
205206

0 commit comments

Comments
 (0)