Skip to content

Commit

Permalink
Merge pull request #1057 from deltachat/add-pagefind-search
Browse files Browse the repository at this point in the history
feat: add required javascript for pagefind in desktop help pages
  • Loading branch information
nicodh authored Feb 24, 2025
2 parents 59d85d3 + fc7d32f commit 46aca50
Showing 1 changed file with 43 additions and 39 deletions.
82 changes: 43 additions & 39 deletions tools/create-local-help.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
#!/usr/bin/env python3

# This script generates the local help files for mobile apps & desktop client

# required arguments:
# srcdir source dir
# destdir destination dir
#
# options:
# --add_top_links : add links back to top of file
# --add_pagefind : add javascript files for page search


# the structure of the help files is:
# - ANY_DIR/help/LANG/help.html (files generated by deltachat-pages)
# - ANY_DIR/help/help.css (file is should be provided by deltachat-UI, not generated by deltachat-pages)


from shutil import copyfile
import sys
import os
import pathlib
import re
import requests
import argparse


# list all files that should go to the local help here.
Expand Down Expand Up @@ -70,7 +80,7 @@ def url_is_reachable(url):
return reachable_cache[url]


def generate_file(srcdir, destdir, lang, file, add_top_links):
def generate_file(srcdir, destdir, lang, file, add_top_links, add_pagefind):
print("generate local help in " + destdir + "/" + lang + "/" + file)

content = read_file(srcdir + "/" + lang + "/" + file)
Expand Down Expand Up @@ -122,6 +132,13 @@ def generate_file(srcdir, destdir, lang, file, add_top_links):
top_link + "<h\\1>",
content,
flags=re.MULTILINE|re.DOTALL) + top_link
if add_pagefind:
content = re.sub(r"</head>",
"<script src=\"./pagefind/pagefind-ui.js\"></script>"
+ "<script src=\"../start-pagefind.js\"></script>"
+ "</head>",
content,
flags=re.MULTILINE|re.DOTALL)

# check that all links are absolute or known relative
urls = re.findall(r"(href|src).*?=.*?\"?([^\">]*)", content)
Expand All @@ -136,7 +153,7 @@ def generate_file(srcdir, destdir, lang, file, add_top_links):
print(f"\033[91m ERROR: link in {lang}/{file} is not reachable: \033[0m {url}")
else:
local_file = destdir + "/" + lang + "/" + url
if not pathlib.Path(local_file).exists():
if not pathlib.Path(local_file).exists() and not url.startswith("./pagefind"):
print(f"\033[91m ERROR: unresolved link in {lang}/{file}: \033[0m {url}")

for anchor in anchors_from_external:
Expand All @@ -146,57 +163,44 @@ def generate_file(srcdir, destdir, lang, file, add_top_links):
write_file(destdir + "/" + lang + "/" + file, content)


def generate_lang(srcdir, destdir, lang, add_top_links):
def generate_lang(srcdir, destdir, lang, add_top_links, add_pagefind):
os.makedirs(destdir + "/" + lang, exist_ok=True)
generate_file(srcdir, destdir, lang, "help.html", add_top_links)
generate_file(srcdir, destdir, lang, "help.html", add_top_links, add_pagefind)


def generate_help(srcdir, destdir, add_top_links=False):
def generate_help(srcdir, destdir, add_top_links, add_pagefind):
for linked_file in linked_files:
srcfile = srcdir + "/" + linked_file
destfile = destdir + "/" + linked_file.split("/")[-1]
print("copy " + srcfile + " to " + destfile)
copyfile(srcfile, destfile)
generate_lang(srcdir, destdir, "cs", add_top_links)
generate_lang(srcdir, destdir, "de", add_top_links)
generate_lang(srcdir, destdir, "en", add_top_links)
generate_lang(srcdir, destdir, "es", add_top_links)
generate_lang(srcdir, destdir, "fr", add_top_links)
generate_lang(srcdir, destdir, "id", add_top_links)
generate_lang(srcdir, destdir, "it", add_top_links)
generate_lang(srcdir, destdir, "pl", add_top_links)
generate_lang(srcdir, destdir, "pt", add_top_links)
generate_lang(srcdir, destdir, "nl", add_top_links)
generate_lang(srcdir, destdir, "ru", add_top_links)
generate_lang(srcdir, destdir, "sk", add_top_links)
generate_lang(srcdir, destdir, "sq", add_top_links)
generate_lang(srcdir, destdir, "uk", add_top_links)
generate_lang(srcdir, destdir, "zh_CN", add_top_links)

languages = ["cs", "de", "en", "es", "fr", "id", "it", "pl", "pt", "nl", "ru", "sk", "sq", "uk", "zh_CN"]
for lang in languages:
generate_lang(srcdir, destdir, lang, add_top_links, add_pagefind)


if __name__ == "__main__":

if len(sys.argv) < 3:
raise SystemExit("usage: create-local-help.py INPUT_DIR OUTPUT_DIR [--add-top-links]"
+"\n eg. create-local-help.py result ../foobar")
parser = argparse.ArgumentParser(description='Parse arguments')

parser.add_argument('srcdir', type=str, help='source dir')
parser.add_argument('destdir', type=str, help='destination dir')

srcdir = sys.argv[1]
print("using source directory: " + srcdir)
parser.add_argument('--add_top_links', action='store_true', help='add links back to top of file')
parser.add_argument('--add_pagefind', action='store_true', help='add javascript files for page search')

destdir = sys.argv[2]
print("using destination directory: " + destdir)
args = parser.parse_args()

add_top_links = False
if len(sys.argv) == 4 and sys.argv[3] == "--add-top-links":
add_top_links = True
print("add links back to top of file: yes")
else:
print("add links back to top of file: no")
print("using source directory: " + args.srcdir)
print("using destination directory: " + args.destdir)
print("add links back to top of file: " + str(args.add_top_links))
print("add pagefind: " + str(args.add_pagefind))

if not os.path.isdir(srcdir):
raise SystemExit("Error: " + srcdir + " is no existent directory.")
if not os.path.isdir(args.srcdir):
raise SystemExit("Error: " + args.srcdir + " is no existent directory.")

if not os.path.isdir(destdir):
raise SystemExit("Error: " + destdir + " is no existent directory.")
if not os.path.isdir(args.destdir):
raise SystemExit("Error: " + args.destdir + " is no existent directory.")

generate_help(srcdir, destdir, add_top_links=add_top_links)
generate_help(args.srcdir, args.destdir, args.add_top_links, args.add_pagefind)

0 comments on commit 46aca50

Please sign in to comment.