Skip to content

Commit d7e2a48

Browse files
committed
Add "source-read" event.
1 parent 39c3121 commit d7e2a48

File tree

4 files changed

+22
-3
lines changed

4 files changed

+22
-3
lines changed

CHANGES

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,8 @@ New features added
8181
- Added ``Sphinx.add_javascript()`` that adds scripts to load in the
8282
default HTML template.
8383

84-
- Added new events: ``env-updated``, ``missing-reference``,
85-
``build-finished``.
84+
- Added new events: ``source-read``, ``env-updated``,
85+
``missing-reference``, ``build-finished``.
8686

8787
* Other changes:
8888

doc/ext/appapi.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,14 @@ registered event handlers.
208208
Emitted when the builder object has been created. It is available as
209209
``app.builder``.
210210

211+
.. event:: source-read (app, docname, source)
212+
213+
Emitted when a source file has been read. The *source* argument is a list
214+
whose single element is the contents of the source file. You can process the
215+
contents and replace this item to implement source-level transformations.
216+
217+
.. versionadded:: 0.5
218+
211219
.. event:: doctree-read (app, doctree)
212220

213221
Emitted when a doctree has been parsed and read by the environment, and is

sphinx/application.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ def __str__(self):
5858
# List of all known core events. Maps name to arguments description.
5959
events = {
6060
'builder-inited': '',
61+
'source-read': 'docname, source text',
6162
'doctree-read': 'the doctree before being pickled',
6263
'missing-reference': 'env, node, contnode',
6364
'doctree-resolved': 'doctree, docname',

sphinx/environment.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -485,8 +485,18 @@ def read_doc(self, docname, src_path=None, save_parsed=True, app=None):
485485
else:
486486
self.warn(docname, 'default role %s not found' %
487487
self.config.default_role)
488+
489+
class SphinxSourceClass(FileInput):
490+
def read(self):
491+
data = FileInput.read(self)
492+
if app:
493+
arg = [data]
494+
app.emit('source-read', docname, arg)
495+
data = arg[0]
496+
return data
497+
488498
self.docname = docname
489-
doctree = publish_doctree(None, src_path, FileInput,
499+
doctree = publish_doctree(None, src_path, SphinxSourceClass,
490500
settings_overrides=self.settings,
491501
reader=SphinxStandaloneReader())
492502
self.filter_messages(doctree)

0 commit comments

Comments
 (0)