Skip to content

Commit a76a462

Browse files
committed
search: bring new module to search across all tracked repositories
Extra also now works on local and offline contexts. Signed-off-by: Jorge Marques <[email protected]>
1 parent 4f239e6 commit a76a462

26 files changed

+1039
-153
lines changed

adi_doctools/lut.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ class Repo(TypedDict):
2626
class LUT(TypedDict):
2727
remote_ssh: str
2828
remote_https: str
29+
remote_doc: str
2930
source_hostname: str
3031
repos: Dict[str, Repo]
3132
banner: Banner
@@ -151,6 +152,7 @@ def get_lut():
151152
# TODO dynamic lut fetch
152153
return LUT(remote_ssh=remote_ssh,
153154
remote_https=remote_https,
155+
remote_doc=remote_doc,
154156
source_hostname=source_hostname,
155157
repos=repos,
156158
banner=banner,

adi_doctools/theme/__init__.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ def theme_config_setup(app):
2525
app.add_config_value('target_depth', None, 'env', [str])
2626

2727
app.connect("config-inited", config_inited)
28+
app.connect("builder-inited", builder_inited)
2829
app.connect("build-finished", build_finished)
2930

3031

@@ -52,6 +53,27 @@ def config_inited(app, config):
5253
if config.target_depth is not None or getenv("ADOC_TARGET_DEPTH", default=None) is not None:
5354
logger.info(f"ADOC_TARGET_DEPTH is deprecated and has no effect.")
5455

56+
def builder_inited(app):
57+
"""
58+
Remove Sphinx's default provided scripts
59+
"""
60+
if app.env.config.html_theme not in names:
61+
return
62+
63+
removal = [
64+
"_static/documentation_options.js",
65+
"_static/doctools.js",
66+
"_static/sphinx_highlight.js"
67+
]
68+
to_remove = []
69+
if app.builder.format == 'html':
70+
for js in app.builder._js_files:
71+
if js.filename in removal:
72+
to_remove.append(js)
73+
for js_ in to_remove:
74+
app.builder._js_files.remove(js_)
75+
76+
5577
def build_finished(app, exc):
5678
if app.builder.format == 'html' and not exc:
5779
if app.env.config.core_repo:
@@ -90,6 +112,7 @@ def build_finished(app, exc):
90112
copy_asset_file(path.join(src_uri, m),
91113
path.join(build_uri, m))
92114

115+
metadata['remote_doc'] = app.lut['remote_doc']
93116
metadata['source_hostname'] = app.lut['source_hostname']
94117

95118
file = path.join(app.builder.outdir, 'metadata.json')

adi_doctools/theme/cosmic/layout.html

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,7 @@
4141
<div class="search-area">
4242
<form action="" method="get">
4343
<input type="text" name="q" aria-labelledby="search-documentation" value="" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" role="search" placeholder="Search"/>
44-
<button value="{{ _('search') }}" class="icon"></button>
45-
<span id="search-progress"></span>
44+
<button class="icon"></button>
4645
</form>
4746
</div>
4847
{% endblock %}
@@ -128,8 +127,6 @@
128127
{%- endblock %}
129128
</div>
130129

131-
<div id="vertical-border"></div>
132-
133130
<label id="cancel-area-show-toc" for="input-show-toc"></label>
134131
<label id="cancel-area-show-localtoc" for="input-show-localtoc"></label>
135132

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import { VersionDropdown } from './version_dropdown.js'
22
import { Links } from './links.js'
33
import { PageActions } from './page_actions.js'
4+
import { Search } from './search.js'
45

56
export default function Extra (){
67
new VersionDropdown(app)
78
new Links(app)
89
new PageActions(app)
10+
new Search(app)
911
}
1012

1113
Extra()

adi_doctools/theme/cosmic/scripts/fetch.js

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,13 @@ export class Fetch {
1818
* that contain the most up-to-date metadata
1919
*/
2020
init () {
21-
if (this.parent.state.offline === true) {
22-
console.log("fetch: dynamic features are not available in offline mode")
23-
return
24-
} else if (this.parent.state.sub_hosted === false) {
25-
console.log("fetch: dynamic features are not available for single repository doc")
26-
return
27-
}
28-
21+
let urls = [
22+
'https://analogdevicesinc.github.io/doctools/metadata.json'
23+
]
24+
if (this.parent.state.offline === false)
25+
urls.unshift('/metadata.json', '/doctools/metadata.json')
2926
Toolbox.cache_check(this.parent.state,
30-
[
31-
'/doctools/metadata.json',
32-
'https://analogdevicesinc.github.io/doctools/metadata.json'
33-
], 24, (obj) => {this.init_metadata(obj)})
27+
urls, 24, (obj) => {this.init_metadata(obj)})
3428
}
3529
/**
3630
* Attach metadata to this and call to inject extra modules.
@@ -54,13 +48,18 @@ export class Fetch {
5448

5549
if (url.startsWith("https://") || url.startsWith("http://"))
5650
url = new URL(url).origin
51+
else if (this.parent.state.offline === true)
52+
url = `${this.parent.state.metadata.remote_doc}/doctools`
5753
else
5854
url = ""
5955

56+
let url_ = this.parent.state.sub_hosted === false ?
57+
`${url}/_static/` : `${url}/doctools/_static/`
58+
6059
if ('javascript' in obj) {
6160
obj['javascript'].forEach((elem) => {
6261
let script = new DOM('script', {
63-
'src': `${url}/doctools/_static/${elem}`
62+
'src': `${url_}${elem}`
6463
});
6564
this.$.head.append(script)
6665
})
@@ -70,7 +69,7 @@ export class Fetch {
7069
let style = new DOM('link', {
7170
'rel': 'stylesheet',
7271
'type': 'text/css',
73-
'href': `${url}/doctools/_static/${elem}`
72+
'href': `${url_}${elem}`
7473
});
7574
this.$.head.append(style)
7675
})

adi_doctools/theme/cosmic/scripts/links.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ export class Links {
3232
update_repotoc (obj) {
3333
let $ = this.$
3434

35+
let prefix = this.parent.state.sub_hosted === true ?
36+
'/' : this.parent.state.metadata.remote_doc
3537
let home = "index.html"
3638
let linksOverlay = [],
3739
linksSidebar = []
@@ -41,7 +43,7 @@ export class Links {
4143

4244
let base = key == this.parent.state.repository ?
4345
this.parent.state.content_root :
44-
`/${key}/`
46+
`${prefix}${key}/`
4547
linksSidebar.push(new DOM('a', {
4648
'href': `${base}${home}`,
4749
'className': this.parent.state.repository === key ? 'current' : '',

adi_doctools/theme/cosmic/scripts/navigation.js

Lines changed: 14 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,6 @@ export class Navigation {
2525
if (this.parent.state.theme !== this.os_theme())
2626
$.body.classList.add(this.parent.state.theme)
2727

28-
$.searchButton = new DOM('button', {
29-
id:'search',
30-
className:'icon',
31-
title:'Search (/)'
32-
}).onclick(this, () => {
33-
DOM.switchState($.searchArea)
34-
DOM.switchState($.searchAreaBg)
35-
$.searchInput.focus()
36-
$.searchInput.$.select()
37-
})
3828
$.changeTheme = new DOM('button', {
3929
className: this.parent.state.theme === 'dark' ? 'icon on' : 'icon',
4030
id:'theme',
@@ -50,22 +40,10 @@ export class Navigation {
5040
}
5141
})
5242

53-
$.searchAreaBg = new DOM('div', {
54-
className:'search-area-bg'
55-
}).onclick(this, () => {
56-
DOM.switchState($.searchArea)
57-
DOM.switchState($.searchAreaBg)
58-
})
59-
$.searchArea = new DOM(DOM.get('.search-area'))
60-
$.searchForm = new DOM(DOM.get('form', $.searchArea))
61-
$.searchInput = new DOM(DOM.get('input', $.searchForm))
62-
$.searchForm.$['action'] = DOM.get('link[rel="search"]').href
63-
$.body.append([$.searchAreaBg])
64-
6543
$.preserve_scroll = {}
6644
$.preserve_scroll['sphinxsidebarwrapper'] = new DOM(DOM.get('.sphinxsidebarwrapper'))
6745

68-
$.rightHeader = new DOM(DOM.get('header #right span.reverse')).append([$.changeTheme, $.searchButton])
46+
$.rightHeader = new DOM(DOM.get('header #right span.reverse')).append([$.changeTheme])
6947

7048
$.relatedNext = DOM.get('.related .next')
7149
$.relatedPrev = DOM.get('.related .prev')
@@ -159,21 +137,6 @@ export class Navigation {
159137
}
160138
}
161139
}
162-
/* Search shortcut */
163-
search (e) {
164-
if ((e.code === 'IntlRo' || e.code === 'Slash')
165-
&& !this.$.searchArea.classList.contains('on')) {
166-
DOM.switchState(this.$.searchArea)
167-
DOM.switchState(this.$.searchAreaBg)
168-
this.$.searchInput.focus()
169-
this.$.searchInput.$.select()
170-
} else if (e.code === 'Escape') {
171-
if (this.$.searchArea.classList.contains('on')) {
172-
DOM.switchState(this.$.searchArea)
173-
DOM.switchState(this.$.searchAreaBg)
174-
}
175-
}
176-
}
177140
/* Related shortcut */
178141
related (e) {
179142
if (!e.altKey || !e.shiftKey)
@@ -200,7 +163,9 @@ export class Navigation {
200163
case 'IntlRo':
201164
case 'Slash':
202165
case 'Escape':
203-
this.search(e)
166+
case 'KeyK':
167+
if (typeof this.parent.search !== "undefined")
168+
this.parent.search.search(e)
204169
break
205170
}
206171
}
@@ -212,7 +177,16 @@ export class Navigation {
212177
case 'ArrowRight':
213178
case 'KeyA':
214179
case 'KeyD':
215-
e.preventDefault()
180+
if (e.altKey && e.shiftKey)
181+
e.preventDefault()
182+
return
183+
case 'IntlRo':
184+
case 'Slash':
185+
return
186+
case 'KeyK':
187+
if (e.ctrlKey && e.altKey)
188+
e.preventDefault()
189+
return
216190
}
217191
}
218192

0 commit comments

Comments
 (0)