Skip to content

Commit

Permalink
ENH: Add Google Custom Search widget
Browse files Browse the repository at this point in the history
Fixes #56
  • Loading branch information
kernc committed Apr 15, 2020
1 parent 155f4ac commit 8535421
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
5 changes: 5 additions & 0 deletions doc/pdoc_template/config.mako
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,9 @@
git_link_template = 'https://github.com/pdoc3/pdoc/blob/{commit}/{path}#L{start_line}-L{end_line}'
search_query = '''
site:pdoc3.github.io
inurl:github.com/pdoc3
'''
%>
6 changes: 6 additions & 0 deletions pdoc/templates/config.mako
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@
# tracking id (UA-XXXXXX-Y).
google_analytics = ''
# If set, insert Google Custom Search search bar widget above the sidebar index.
# The whitespace-separated tokens represent arbitrary extra queries (at least one
# must match) passed to regular Google search. Example:
#search_query = 'inurl:github.com/USER/PROJECT site:PROJECT.github.io site:PROJECT.website'
search_query = ''
# If set, render LaTeX math syntax within \(...\) (inline equations),
# or within \[...\] or $$...$$ or `.. math::` (block equations)
# as nicely-formatted math formulas using MathJax.
Expand Down
12 changes: 12 additions & 0 deletions pdoc/templates/html.mako
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,13 @@
<%include file="logo.mako"/>
% if search_query:
<div class="gcse-search" style="height: 70px"
data-as_oq="${' '.join(search_query.strip().split()) | h }"
data-gaCategoryParameter="${module.refname | h}">
</div>
% endif
<h1>Index</h1>
${extract_toc(module.docstring) if extract_module_toc_into_sidebar else ''}
<ul id="index">
Expand Down Expand Up @@ -384,6 +391,11 @@
</script><script async src='https://www.google-analytics.com/analytics.js'></script>
% endif

% if search_query:
<script async src="https://cse.google.com/cse.js?cx=017837193012385208679:pey8ky8gdqw"></script>
<style>.gsc-control-cse {padding:0 !important;margin-top:1em}</style>
% endif

% if latex_math:
<script async src='https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/latest.js?config=TeX-AMS_CHTML'></script>
% endif
Expand Down

4 comments on commit 8535421

@evyatartzik
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi kernc how can i use this feature on pdoc cli so that my genrated HTML will have a search widget?

@kernc
Copy link
Member Author

@kernc kernc commented on 8535421 Apr 16, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@evyatartzik Simply override search_query template variable with your additional relevant queries. Example, using Google's advanced search operators:

pdoc --config 'search_query="inurl:github.com/USER/PROJECT  site:PROJECT.github.io"' ...

As this relies on Google search, your intended results obviously must be public on the web and already indexed.

@evyatartzik
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ValueError: Error evaluating --config statement "search_query=inurl:github.com/USER/PROJECT site:PROJECT.github.io". Make sure string values are quoted?

@kernc
Copy link
Member Author

@kernc kernc commented on 8535421 Apr 16, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, maybe put additional quotes around the whole thing: 😁

pdoc --config 'search_query="inurl:github.com/USER/PROJECT  site:PROJECT.github.io"' ...

Please sign in to comment.