Skip to content

Commit 151da4c

Browse files
committed
Add PWA Support
1 parent 9fec1c2 commit 151da4c

File tree

8 files changed

+171
-3
lines changed

8 files changed

+171
-3
lines changed

source/_extensions/pwa_service.py

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
import sphinx as Sphinx
2+
from typing import Any, Dict, List
3+
import os
4+
from docutils import nodes
5+
import json
6+
import shutil
7+
8+
manifest = {
9+
"name": "",
10+
"short_name": "",
11+
"theme_color": "",
12+
"background_color": "",
13+
"display": "standalone",
14+
"scope": "/",
15+
"start_url": "/index.html",
16+
"icons": [],
17+
}
18+
19+
20+
def get_files_to_cache(outDir: str):
21+
files_to_cache = []
22+
for (dirpath, dirname, filenames) in os.walk(outDir):
23+
dirpath = dirpath.split(outDir)[1]
24+
25+
# skip adding sources to cache
26+
if os.sep + "_sources" + os.sep in dirpath:
27+
continue
28+
29+
# add files to cache
30+
for name in filenames:
31+
if "sw.js" in name:
32+
continue
33+
34+
dirpath = dirpath.replace("\\", "/")
35+
dirpath = dirpath.lstrip("/")
36+
if dirpath == "":
37+
files_to_cache.append(name)
38+
else:
39+
files_to_cache.append(dirpath + "/" + name)
40+
41+
return files_to_cache
42+
43+
44+
def build_finished(app: Sphinx, exception: Exception):
45+
outDir = app.outdir
46+
outDirStatic = outDir + os.sep + "_static" + os.sep
47+
files_to_cache = get_files_to_cache(outDir)
48+
49+
# dumps a json file with our cache
50+
with open(outDirStatic + "cache.json", "w") as f:
51+
json.dump(files_to_cache, f)
52+
53+
# copies over our service worker
54+
shutil.copyfile(
55+
os.path.dirname(__file__) + os.sep + "pwa_service_files" + os.sep + "sw.js",
56+
outDir + os.sep + "sw.js",
57+
)
58+
59+
60+
def html_page_context(
61+
app: Sphinx,
62+
pagename: str,
63+
templatename: str,
64+
context: Dict[str, Any],
65+
doctree: nodes.document,
66+
) -> None:
67+
app.add_js_file(
68+
None,
69+
body='"serviceWorker"in navigator&&navigator.serviceWorker.register("sw.js");',
70+
loading_method="defer",
71+
)
72+
if pagename == "index":
73+
context[
74+
"metatags"
75+
] += '<script>"serviceWorker"in navigator&&navigator.serviceWorker.register("sw.js");</script>'
76+
context[
77+
"metatags"
78+
] += f'<link rel="manifest" href="_static/frcdocs.webmanifest"/>'
79+
80+
81+
def setup(app: Sphinx) -> Dict[str, Any]:
82+
app.add_config_value("name", "", "html")
83+
app.add_config_value("short_name", "", "html")
84+
app.add_config_value("theme_color", "", "html")
85+
app.add_config_value("background_color", "", "html")
86+
app.add_config_value("display", "standalone", "html")
87+
app.add_config_value("icons", [], "html")
88+
89+
app.connect("html-page-context", html_page_context)
90+
app.connect("build-finished", build_finished)
91+
92+
return {
93+
"parallel_read_safe": True,
94+
"parallel_write_safe": True,
95+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// extend this to update the service worker every push
2+
// https://developer.mozilla.org/en-US/docs/Web/Progressive_web_apps/Offline_Service_workers
3+
var cacheName = 'js13kPWA-v1';
4+
5+
self.addEventListener('install', function(e) {
6+
e.waitUntil(
7+
caches.open('frc-docs').then(function(cache) {
8+
fetch('_static/cache.json')
9+
.then(response => response.json())
10+
.then(async (data) => {
11+
for (let i = 0; i < data.length / 10; i++) {
12+
const tofetch = data.slice(i * 10, i * 10 + 9)
13+
await cache.addAll(tofetch)
14+
}
15+
})
16+
.catch(error => {
17+
console.error(error);
18+
});
19+
})
20+
);
21+
});
22+
23+
self.addEventListener('fetch', function(event) {
24+
event.respondWith(
25+
caches.open('frc-docs').then(function(cache) {
26+
return cache.match(event.request).then(function (response) {
27+
return response || fetch(event.request).then(function(response) {
28+
cache.put(event.request, response.clone());
29+
return response;
30+
});
31+
});
32+
})
33+
);
34+
});

source/_static/first-logo-256px.png

19.7 KB
Loading

source/_static/first-logo-512px.png

43.3 KB
Loading

source/_static/frcdocs.webmanifest

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "FRC Docs",
3+
"short_name": "FRC Docs",
4+
"theme_color": "#003974",
5+
"background_color": "#003974",
6+
"display": "standalone",
7+
"scope": "/",
8+
"start_url": "/index.html",
9+
"icons": [
10+
{
11+
"src": "/_static/first-logo-256px.png",
12+
"type": "image/png",
13+
"sizes": "256x256"
14+
},
15+
{
16+
"src": "/_static/first-logo-512px.png",
17+
"type": "image/png",
18+
"sizes": "512x512"
19+
}
20+
]
21+
}

source/_static/sw.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
self.addEventListener('install', function(e) {
2+
e.waitUntil(
3+
caches.open('frc-docs').then(function(cache) {
4+
return cache.addAll([
5+
'/',
6+
'/index.html'
7+
]);
8+
})
9+
);
10+
});
11+
12+
self.addEventListener('fetch', function(event) {
13+
event.respondWith(
14+
caches.match(event.request).then(function(response) {
15+
return response || fetch(event.request);
16+
})
17+
);
18+
});

source/conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
"_extensions.post_process",
6060
"_extensions.rtd_patch",
6161
"_extensions.localization",
62+
"_extensions.pwa_service",
6263
]
6364

6465
extensions += local_extensions
@@ -112,7 +113,6 @@
112113
ogp_custom_meta_tags = [
113114
'<meta property="og:ignore_canonical" content="true" />',
114115
'<meta name="theme-color" content="#003974" />',
115-
'<meta name="google-site-verification" content="xcmdxM0KzMYiAOeJzyF_l2Tn3AHGzPICs9_vX6q2nwM" />',
116116
]
117117

118118
# Set location of pages to be indexed by delta

source/docs/software/advanced-controls/state-space/graph.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ def lqr(*args, **kwargs):
6060
x1 = np.array([[0]])
6161
x2 = np.array([[0]])
6262

63-
K1 = lqr(sysd, np.array([[1.0 / (1.0**2)]]), np.array([[1.0 / (12.0**2)]]))
64-
K2 = lqr(sysd, np.array([[1.0 / (0.1**2)]]), np.array([[1.0 / (12.0**2)]]))
63+
K1 = lqr(sysd, np.array([[1.0 / (1.0 ** 2)]]), np.array([[1.0 / (12.0 ** 2)]]))
64+
K2 = lqr(sysd, np.array([[1.0 / (0.1 ** 2)]]), np.array([[1.0 / (12.0 ** 2)]]))
6565

6666
t1 = []
6767
x1data = []

0 commit comments

Comments
 (0)