Skip to content

Commit

Permalink
Add: search-bar that uses DuckDuckGo to search the wiki (#96)
Browse files Browse the repository at this point in the history
It requires the --frontend-url to be set, as otherwise we cannot
scope the DuckDuckGo results.
  • Loading branch information
TrueBrain authored Nov 23, 2020
1 parent e0a6b70 commit 2b0ce6d
Show file tree
Hide file tree
Showing 16 changed files with 196 additions and 1 deletion.
45 changes: 44 additions & 1 deletion static/truewiki/truewiki.css
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ header {
#review-access {
position: absolute;
right: 30px;
top: 10px;
top: 70px;
}
#review-access a {
color: #aaa;
Expand All @@ -504,3 +504,46 @@ header {
font-size: 70%;
margin: -4px 0 4px 0;
}

#search {
position: absolute;
right: 30px;
top: 10px;
}
#search input[type=text] {
width: 150px;
}

#search-submit {
display: inline-block;
position: absolute;
right: 0px;
}
#search-submit input {
background-color: white;
border: 0;
border-left: 1px solid #aaa;
height: 20px;
margin: 1px 1px 0 0;
width: 20px;
}
#search-submit:hover input {
background-color: #aaa;
}
#search-submit div {
display: inline-block;
pointer-events: none;
position: absolute;
right: 1px;
top: -1px;
transform: rotate(45deg);
}
#search-submit div::after {
color: #D52D2D;
content: "⚲";
font-size: 18px;
font-weight: bold;
left: -4px;
position: relative;
top: 4px;
}
14 changes: 14 additions & 0 deletions templates/Edit.mediawiki
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,20 @@
<a href="{{{user_settings_url}}}">Review access</a>
</div>
}}
{{#if: {{{has_search|}}}|
<div id="search">
<form action="/search" target="_new">
{{#if: {{{language|}}}|
<input type="hidden" name="language" value="{{{language}}}" />
}}
<input type="text" autocomplete="off" name="query" value="" placeholder="Search wiki" />
<div id="search-submit">
<input type="submit" value="" />
<div></div>
</div>
</form>
</div>
}}
</header>
<nav>
<ul id="navigation-bar">
Expand Down
14 changes: 14 additions & 0 deletions templates/Error.mediawiki
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,20 @@
Error
</div>
{{{html_header}}}
{{#if: {{{has_search|}}}|
<div id="search">
<form action="/search" target="_new">
{{#if: {{{language|}}}|
<input type="hidden" name="language" value="{{{language}}}" />
}}
<input type="text" autocomplete="off" name="query" value="" placeholder="Search wiki" />
<div id="search-submit">
<input type="submit" value="" />
<div></div>
</div>
</form>
</div>
}}
</header>
<nav>
<ul id="navigation-bar">
Expand Down
14 changes: 14 additions & 0 deletions templates/Login.mediawiki
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,20 @@
<a href="{{{user_settings_url}}}">Review access</a>
</div>
}}
{{#if: {{{has_search|}}}|
<div id="search">
<form action="/search" target="_new">
{{#if: {{{language|}}}|
<input type="hidden" name="language" value="{{{language}}}" />
}}
<input type="text" autocomplete="off" name="query" value="" placeholder="Search wiki" />
<div id="search-submit">
<input type="submit" value="" />
<div></div>
</div>
</form>
</div>
}}
</header>
<nav>
<ul id="navigation-bar" class="right">
Expand Down
14 changes: 14 additions & 0 deletions templates/Page.mediawiki
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,20 @@
<a href="{{{user_settings_url}}}">Review access</a>
</div>
}}
{{#if: {{{has_search|}}}|
<div id="search">
<form action="/search" target="_new">
{{#if: {{{language|}}}|
<input type="hidden" name="language" value="{{{language}}}" />
}}
<input type="text" autocomplete="off" name="query" value="" placeholder="Search wiki" />
<div id="search-submit">
<input type="submit" value="" />
<div></div>
</div>
</form>
</div>
}}
</header>
<nav>
<ul id="navigation-bar">
Expand Down
14 changes: 14 additions & 0 deletions templates/Source.mediawiki
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,20 @@
<a href="{{{user_settings_url}}}">Review access</a>
</div>
}}
{{#if: {{{has_search|}}}|
<div id="search">
<form action="/search" target="_new">
{{#if: {{{language|}}}|
<input type="hidden" name="language" value="{{{language}}}" />
}}
<input type="text" autocomplete="off" name="query" value="" placeholder="Search wiki" />
<div id="search-submit">
<input type="submit" value="" />
<div></div>
</div>
</form>
</div>
}}
</header>
<nav>
<ul id="navigation-bar">
Expand Down
4 changes: 4 additions & 0 deletions truewiki/namespaces/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ def get_used_on_pages(page: str) -> list:
def page_get_correct_case(page: str) -> str:
return metadata.PAGES_LC.get(page.lower(), page)

@staticmethod
def page_get_language(page: str) -> str:
return None

@staticmethod
def get_create_page_name(page: str) -> str:
return ""
Expand Down
9 changes: 9 additions & 0 deletions truewiki/namespaces/category/namespace.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,15 @@ def page_is_valid(cls, page: str, is_new_page: bool) -> Optional[str]:

return None

@classmethod
def page_get_language(cls, page: str) -> Optional[str]:
assert page.startswith("Category/")

if cls._is_root(page):
return None

return page.split("/")[1]

@classmethod
def has_source(cls, page: str) -> bool:
return not cls._is_root(page) and not cls._is_root_of_folder(page)
Expand Down
9 changes: 9 additions & 0 deletions truewiki/namespaces/file/namespace.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,15 @@ def page_is_valid(cls, page: str, is_new_page: bool) -> Optional[str]:

return None

@classmethod
def page_get_language(cls, page: str) -> Optional[str]:
assert page.startswith("File/")

if cls._is_root(page):
return None

return page.split("/")[1]

@staticmethod
def get_used_on_pages(page: str) -> list:
assert page.startswith("File/")
Expand Down
11 changes: 11 additions & 0 deletions truewiki/namespaces/folder/namespace.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,17 @@ def page_is_valid(cls, page: str, is_new_page: bool) -> Optional[str]:
def page_ondisk_name(page: str) -> str:
return None

@classmethod
def page_get_language(cls, page: str) -> Optional[str]:
assert page.startswith("Folder/")

if cls._is_root(page):
return None
if cls._is_namespace_root(page):
return None

return page.split("/")[2]

@staticmethod
def get_used_on_pages(page: str) -> list:
return []
Expand Down
6 changes: 6 additions & 0 deletions truewiki/namespaces/page/namespace.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ def page_get_correct_case(cls, page: str) -> str:
correct_page = correct_page[len("Page/") :]
return correct_page

@classmethod
def page_get_language(cls, page: str) -> Optional[str]:
if "/" not in page:
return None
return page.split("/")[0]

@staticmethod
def has_source(page: str) -> bool:
return True
Expand Down
9 changes: 9 additions & 0 deletions truewiki/namespaces/template/namespace.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,15 @@ def page_is_valid(cls, page: str, is_new_page: bool) -> Optional[str]:

return None

@classmethod
def page_get_language(cls, page: str) -> Optional[str]:
assert page.startswith("Template/")

if cls._is_root(page):
return None

return page.split("/")[1]

@classmethod
def has_source(cls, page: str) -> bool:
return not cls._is_root(page) and not cls._is_language_root(page)
Expand Down
3 changes: 3 additions & 0 deletions truewiki/views/sitemap.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,7 @@ def invalidate_cache() -> None:
def click_sitemap(frontend_url):
global FRONTEND_URL

if frontend_url.endswith("/"):
frontend_url = frontend_url[:-1]

FRONTEND_URL = frontend_url
23 changes: 23 additions & 0 deletions truewiki/web_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,29 @@ async def robots(request):
return web.Response(body="User-agent: *", content_type="text/plain")


@routes.get("/search")
@csp_header
async def search(request):
site = sitemap.FRONTEND_URL
if not site:
return web.HTTPNotFound()

if site.startswith("http://"):
site = site[len("http://") :]
elif site.startswith("https://"):
site = site[len("https://") :]

query = request.query.get("query", "")
if query:
query += " "

language = request.query.get("language", "")
if language:
language = f"%2F{language}%2F"

return web.HTTPFound(f"https://duckduckgo.com/?q={query}site%3A{site}{language}")


@routes.get("/edit/{page:.*}")
@csp_header
async def edit_page(request):
Expand Down
4 changes: 4 additions & 0 deletions truewiki/wiki_page.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,10 @@ def page_get_correct_case(self, page: str) -> str:
namespace = page.split("/")[0]
return NAMESPACES.get(namespace, NAMESPACE_DEFAULT_PAGE).page_get_correct_case(page)

def page_get_language(self, page: str) -> Optional[str]:
namespace = page.split("/")[0]
return NAMESPACES.get(namespace, NAMESPACE_DEFAULT_PAGE).page_get_language(page)

def has_source(self, page: str) -> bool:
# "License" is a special page, of which we never show the source.
if page == "License":
Expand Down
4 changes: 4 additions & 0 deletions truewiki/wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
config,
singleton,
)
from .views import sitemap
from .wiki_page import WikiPage


Expand All @@ -31,6 +32,9 @@ def wrap_page(page, wrapper, variables, templates):
variables["create_page_name"] = wiki_page.get_create_page_name(page)
variables["repository_url"] = singleton.STORAGE.get_repository_url()

variables["language"] = wiki_page.page_get_language(page) or ""
variables["has_search"] = "1" if sitemap.FRONTEND_URL else ""

variables["css"] = config.HTML_SNIPPETS["css"]
variables["favicon"] = config.FAVICON
variables["html_footer"] = config.HTML_SNIPPETS["footer"]
Expand Down

0 comments on commit 2b0ce6d

Please sign in to comment.