Skip to content

Commit 172080b

Browse files
authored
minify HTML (#4)
* minify HTML * fix typo * keep comments * adjust options * adjust options * adjust options * fix banner typo * adjust options
1 parent ce59434 commit 172080b

File tree

4 files changed

+33
-5
lines changed

4 files changed

+33
-5
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
This package should be used in all sphinx projects under the [@deepmodeling](https://github.com/deepmodeling) organization.
44

5+
## Features
6+
7+
- Adding the DeepModeling banner
8+
- Minify HTML files
9+
510
## How to use it
611

712
Add `deepmodeling_sphinx` to the requirements, as well as the `extensions` of `conf.py`:

deepmodeling_sphinx/banner.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
<p></p>
4545
</a>
4646
</div>
47+
</div>
4748
</li>
4849

4950
<li>

deepmodeling_sphinx/inject.py

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from typing import Dict, Any
33
import os
44

5+
import minify_html
56
from sphinx.application import Sphinx
67
from sphinx.util.fileutil import copy_asset_file
78

@@ -33,7 +34,7 @@ def insert_sidebar(app, pagename, templatename, context, doctree):
3334
):
3435
old_render = app.builder.templates.render
3536

36-
def rtd_render(self, template, render_context):
37+
def render(self, template, render_context):
3738
content = old_render(template, render_context)
3839
comment_begin = r"<!--deepmodeling begin-->"
3940
comment_end = r"<!--deepmodeling end-->"
@@ -47,16 +48,34 @@ def rtd_render(self, template, render_context):
4748
with open(source) as f:
4849
banner = f.read()
4950
if begin_body != -1:
50-
content = content[:begin_body] + comment_begin + banner + comment_end + content[begin_body:]
51+
content = content[:begin_body] + comment_begin + \
52+
banner + comment_end + content[begin_body:]
5153
return content
5254

53-
rtd_render._deepmodeling_patched = True
54-
app.builder.templates.render = types.MethodType(rtd_render,
55+
render._deepmodeling_patched = True
56+
app.builder.templates.render = types.MethodType(render,
57+
app.builder.templates)
58+
59+
60+
def minify_html_files(app, pagename, templatename, context, doctree):
61+
if (
62+
not hasattr(app.builder.templates.render, '_deepmodeling_minified')
63+
):
64+
old_render = app.builder.templates.render
65+
66+
def render(self, template, render_context):
67+
content = old_render(template, render_context)
68+
return minify_html.minify(content, minify_css=True, minify_js=True,
69+
)
70+
71+
render._deepmodeling_minified = True
72+
app.builder.templates.render = types.MethodType(render,
5573
app.builder.templates)
5674

5775

5876
def setup(app: Sphinx) -> Dict[str, Any]:
5977
app.connect('builder-inited', copy_custom_files)
6078
app.connect('html-page-context', insert_sidebar)
79+
app.connect('html-page-context', minify_html_files)
6180

6281
return {'parallel_read_safe': True}

setup.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55
use_scm_version=True,
66
setup_requires=['setuptools_scm'],
77
packages=['deepmodeling_sphinx'],
8-
install_require=['sphinx'],
8+
install_requires=[
9+
'sphinx',
10+
'minify-html',
11+
],
912
package_data={
1013
'deepmodeling_sphinx': ['banner.html',
1114
'banner.css',

0 commit comments

Comments
 (0)