From 69bf8476cb2b1925291d52ea021757bfe0d4987a Mon Sep 17 00:00:00 2001 From: zPlus Date: Sat, 25 Nov 2023 16:32:53 +0100 Subject: [PATCH] Adapt manpages to new graph. The graph for manpages has changed. This commit changes all the queries for the new graph. --- README | 2 +- app.py | 175 +++++++++++--------- fuseki.service | 3 +- pages/manpages.html | 17 +- pages/templates/manpages/disambiguation.tpl | 12 +- pages/templates/manpages/distribution.tpl | 4 +- pages/templates/manpages/manpage.tpl | 10 +- website.service | 22 +++ 8 files changed, 143 insertions(+), 102 deletions(-) create mode 100644 website.service diff --git a/README b/README index 0f4b030..e92f35a 100644 --- a/README +++ b/README @@ -25,7 +25,7 @@ for configuring paths or other custom settings. python3 -m venv venv source venv/bin/activate pip install -r requirements.txt - gunicorn --reload --worker-connections=4 --threads=4 --bind 0.0.0.0:8080 app:application + gunicorn --reload --worker-connections=4 --threads=4 --bind 0.0.0.0:8080 --error-logfile=- app:application # Crawl website for static HTML pages: diff --git a/app.py b/app.py index 7904d78..cf85123 100755 --- a/app.py +++ b/app.py @@ -146,15 +146,17 @@ def manpages_distribution(distribution, version): f''' PREFIX mp: - SELECT DISTINCT ?package + SELECT DISTINCT ?name WHERE {{ - ?id mp:source [ - mp:distribution_name "{distribution}" ; - mp:distribution_version "{version}" ; - mp:package ?package ; - ] . + [] a mp:Distribution ; + mp:name "{distribution}" ; + mp:number {version} ; + mp:package ?package . + + ?package a mp:Package ; + mp:name ?name . }} - ORDER BY ?package + ORDER BY ?name ''') return template('templates/manpages/distribution.tpl', data=data, distribution=distribution, version=version) @@ -169,102 +171,121 @@ def manpages_package(distribution, version, package): f''' PREFIX mp: - SELECT ?id ?filename + SELECT (?page as ?id) ?filename WHERE {{ - ?id mp:source [ - mp:package "{package}" ; - mp:distribution_version "{version}" ; - mp:distribution_name "{distribution}" ; - mp:filename ?filename ; - ] . + [] a mp:Distribution ; + mp:name "{distribution}" ; + mp:number {version} ; + mp:package ?package . + + ?package a mp:Package ; + mp:name "{package}" ; + mp:page ?page . + + ?page a mp:Page ; + mp:filename ?filename }} ORDER BY ?filename ''') return template('templates/manpages/package.tpl', data=data, distribution=distribution, version=version, package=package) -@bottle.get('/manpages////.
.', name='manpages_page') -def manpages_page(distribution, version, package, name, section, lang): +@bottle.get('/manpages/.
', name='manpages_disambiguation') +def manpages_disambiguation(name, section): """ - Display a single manpage. + Show a list of manpages that match and
""" - page_id = f'' - - # These characters where url-encoded when creating the RDF data because they - # make invalid URIs. This means for example that in the graph the URIs will be - # encoded with %23 instead of # - page_id = page_id.replace(' ', '_') \ - .replace('[', '%5B') \ - .replace(']', '%5D') \ - .replace('#', '%23') - data = query( f''' PREFIX mp: - DESCRIBE {page_id} - ''', - { - '@context': { - 'mp': 'dokk:manpages:', - }, - 'mp:source': {}, - }) - - # Replace references to other manpages with links. - # Manpages have a section "See also" at the bottom where they suggest other pages. - # For example: - # SEE ALSO - # cdk(3), cdk_screen(3), cdk_display(3), cdk_binding(3), cdk_util(3) - # We convert these strings to links to other pages. - # Example: ls(1) -> ls(1) - # regex explanation: - # - some manpages use a bold or italic name, so we match an optional - # tag - # - match valid characters for a manpage, assign name - # - match optional closing or tags - # - match '(' - # - match number, assign name
- # - match ')' - html = data['@graph'][0]['mp:html'] - html = re.sub ( - '(?:|)?(?P[\w_.:-]+)(?:|)?\((?P
[0-9]\w*)\)', - lambda match: - f'''{match.group('page')}({match.group('section')})''', - html) - - data['@graph'][0]['mp:html'] = html + SELECT * + WHERE {{ + ?page a mp:Page ; + mp:filename ?filename ; + mp:name_lowercase "{name}" ; + mp:section_lowercase "{section}" . + + ?package a mp:Package ; + mp:name ?package_name ; + mp:page ?page . + + ?distro a mp:Distribution ; + mp:name ?distro_name ; + mp:number ?distro_number ; + mp:package ?package . + }} + ORDER BY ?distro_name ?distro_number ?package_name + ''')['results']['bindings'] - return template('templates/manpages/manpage.tpl', data=data['@graph'][0], distribution=distribution, - version=version, package=package, name=name, section=section, lang=lang) + return template('templates/manpages/disambiguation.tpl', name=name, section=section, data=data) -@bottle.get('/manpages/.
', name='manpages_disambiguation') -def manpages_disambiguation(name, section): +@bottle.get('/manpages////', name='manpages_page') +def manpages_page(distro_name, distro_number, package, page): """ - Show a list of manpages that match and
+ Display a single manpage. """ data = query( f''' PREFIX mp: - DESCRIBE ?page + SELECT * WHERE {{ - ?page mp:name_lowercase "{name}" ; - mp:section_lowercase "{section}" . + ?page a mp:Page ; + mp:filename "{page}" ; + mp:name ?page_name ; + mp:section ?page_section ; + mp:html ?page_html . + + ?package a mp:Package ; + mp:name "{package}" ; + mp:page ?page . + + ?distro a mp:Distribution ; + mp:name "{distro_name}" ; + mp:number {distro_number} ; + mp:package ?package . }} - ''', - { - '@context': { - 'mp': 'dokk:manpages:', - }, - 'mp:source': {}, - }) + ''')['results']['bindings'][0] + + """ + Replace references to other manpages with links. + Manpages have a section "See also" at the bottom where they suggest other pages. + For example: + SEE ALSO + cdk(3), cdk_screen(3), cdk_display(3), cdk_binding(3), cdk_util(3) + We convert these strings to links to other pages. + Example: ls(1) -> ls(1) + regex explanation: + - some manpages use a bold or italic name, so we match an optional + tag + - match valid characters for a manpage, assign name + - match optional closing or tags + - match '(' + - match number, assign name
+ - match ')' + """ + + html = data['page_html']['value'] + html = re.sub ( + '(?:|)?(?P[\w_.:-]+)(?:|)?\((?P
[0-9]\w*)\)', + lambda match: + f'''{match.group('page')}({match.group('section')})''', + html) - return template('templates/manpages/disambiguation.tpl', name=name, section=section, data=data) + return template('templates/manpages/manpage.tpl', + distro = {'name': distro_name, 'number': distro_number}, + package = package, + page = { + 'filename': page, + 'name': data['page_name']['value'], + 'section': data['page_section']['value'], + 'html': html, + }) @bottle.get('/mimi_and_eunice/', name='mimi_and_eunice_strip') def mimi_and_eunice_strip(number): diff --git a/fuseki.service b/fuseki.service index ee76775..792d751 100644 --- a/fuseki.service +++ b/fuseki.service @@ -1,5 +1,4 @@ -# This is an example service file for systemd. If using the Fuseki server, this -# service can be configured to automatically start the server. +# systemd service file for starting the Fuseki server # # How to use this file: # - copy this file to /etc/systemd/system diff --git a/pages/manpages.html b/pages/manpages.html index 72c9b2a..7bb4714 100644 --- a/pages/manpages.html +++ b/pages/manpages.html @@ -3,14 +3,13 @@ {% set data = query(""" PREFIX mp: - SELECT DISTINCT ?distribution ?version + SELECT ?name ?number WHERE { - [] mp:source [ - mp:distribution_version ?version ; - mp:distribution_name ?distribution ; - ] . + [] a mp:Distribution ; + mp:name ?name ; + mp:number ?number } - ORDER BY ?distribution ?version + ORDER BY ?name ?number """) %} @@ -31,9 +30,9 @@ {% for result in data["results"]["bindings"] %} {% endfor %} diff --git a/pages/templates/manpages/disambiguation.tpl b/pages/templates/manpages/disambiguation.tpl index 5c046ff..7b4bd3d 100644 --- a/pages/templates/manpages/disambiguation.tpl +++ b/pages/templates/manpages/disambiguation.tpl @@ -17,18 +17,18 @@

- {% for page in data["@graph"] %} + {% for result in data %} {% endfor %} diff --git a/pages/templates/manpages/distribution.tpl b/pages/templates/manpages/distribution.tpl index a5c36df..f403bec 100644 --- a/pages/templates/manpages/distribution.tpl +++ b/pages/templates/manpages/distribution.tpl @@ -16,9 +16,9 @@ Packages:

- {% for page in data['results']['bindings'] %} + {% for package in data['results']['bindings'] %} {% endfor %} diff --git a/pages/templates/manpages/manpage.tpl b/pages/templates/manpages/manpage.tpl index cbbff47..a0e850d 100644 --- a/pages/templates/manpages/manpage.tpl +++ b/pages/templates/manpages/manpage.tpl @@ -1,6 +1,6 @@ {% extends "templates/base.tpl" %} -{% block title %}{{ data['mp:name'] }}({{ data['mp:section'] }}){% endblock %} +{% block title %}{{ page["name"] }}({{ page["section"] }}){% endblock %} {% block stylesheets %} {% endblock %} @@ -12,12 +12,12 @@
DOKK / manpages / - {{ distribution }} {{ version }} / - {{ package }} / - {{ data["mp:source"]["mp:filename"] }} + {{ distro["name"] }} {{ distro["number"] }} / + {{ package }} / + {{ page["filename"] }}
-
{{ data["mp:html"]|safe }}
+
{{ page["html"]|safe }}
diff --git a/website.service b/website.service new file mode 100644 index 0000000..0586f33 --- /dev/null +++ b/website.service @@ -0,0 +1,22 @@ +# systemd service file for starting the web app +# +# How to use this file: +# - copy this file to /etc/systemd/system +# - edit the variables below accordingly +# - systemctl enable website.service +# - systemctl start website.service + +[Unit] +Description=DOKK website app +After=network.target + +[Service] +ExecStart=/home/fuseki/dokk.org/venv/bin/gunicorn --bind localhost:8080 app:application + +User=fuseki +Group=fuseki +WorkingDirectory=/home/fuseki/dokk.org +Restart=always + +[Install] +WantedBy=multi-user.target
- - {{ page['mp:source']['mp:filename'] }} + + {{ result['filename']['value'] }} - {{ page['mp:source']['mp:distribution_name'] }} - {{ page['mp:source']['mp:distribution_version'] }} + {{ result['distro_name']['value'] }} + {{ result['distro_number']['value'] }} / - {{ page['mp:source']['mp:package'] }} + {{ result['package_name']['value'] }}