home » zplus/dokk.org.git
ID: 056a7373cc9363a20eec77223cf6eab3bc4e70e4
462 lines — 12K — View raw


  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
#!/usr/bin/env python3

import bottle
import datetime
import dateutil
import functools
import jinja2
import pyld
import re
import requests

from bottle import jinja2_template as template, request, response
from string import Template

# This only exists for exporting the bottle app object for a WSGI server such as Gunicorn
application = bottle.app()

# Directories to search for HTML templates
bottle.TEMPLATE_PATH = [ './templates' ]

def query(query_string, jsonld_frame=None):
    """
    Send query to Fuseki via HTTP.
    Return the query results.
    """

    http_request = requests.post(
        'http://localhost:7000/dokk',
        data = { 'format': 'json', 'query': query_string})

    results = http_request.json()

    if jsonld_frame:
        results = pyld.jsonld.frame(results, jsonld_frame, options={'omitGraph':False})

    return results

def human_size(bytes, B=False):
    """
    Convert a file size in bytes to a human friendly form.
    This is only used in templates when showing file sizes.
    """

    for unit in [ 'B' if B else '', 'K', 'M', 'G', 'T', 'P' ]:
        if bytes < 1024: break
        bytes = bytes / 1024

    return '{}{}'.format(round(bytes), unit).rjust(5)

template = functools.partial(template, template_settings = {
    'filters': {
        'datetime': lambda date: dateutil.parser.parse(date).strftime('%b %-d, %Y - %H:%M%z%Z'),
        'human_size': human_size
    },
    'globals': {
        'now': lambda: datetime.datetime.now(datetime.timezone.utc),
        'request': request,
        'url': application.get_url,
    },
    'autoescape': True
})

@bottle.error(404)
def error404(error):
    """
    Custom 404 page.

    :param error: bottle.HTTPError given by Bottle when calling abort(404).
    """

    return '[404] {}'.format(error.body)

@bottle.get('/static/<filename:path>', name='static')
def static(filename):
    """
    Path for serving static files.
    """

    return bottle.static_file(filename, root='./static/')

@bottle.get('/', name='homepage')
def homepage():
    """
    """

    return template('index.html')

@bottle.get('/library', name='library')
def library():
    """
    """

    data = query(
    '''
    PREFIX library: <dokk:library:>
    PREFIX license: <dokk:license:>

    CONSTRUCT {
        ?item library:title ?title;
              library:author ?author ;
              library:license ?license .
        ?license license:id ?license_id ;
                 license:name ?license_name .
    }
    WHERE {
        ?item library:title ?title ;
                library:author ?author ;
                library:license ?license .

        OPTIONAL {
        ?license license:id ?license_id_optional ;
                 license:name ?license_name_optional .
        }

        BIND(COALESCE(?license_id_optional, SUBSTR(STR(?license), 14)) AS ?license_id)
        BIND(COALESCE(?license_name_optional, SUBSTR(STR(?license), 14)) AS ?license_name)
    }
    ORDER BY UCASE(?title)
    ''',
    {
        '@context': {
            'library': 'dokk:library:',
            'license': 'dokk:license:',
            'library:author': { '@container': '@set' },
            'library:license': { '@container': '@set' }
        },
        'library:title': {}
    })

    return template('library.html', data=data)

@bottle.get('/library/<item_id>', name='library_item')
def library_item(item_id):
    """
    """

    item_iri = '<dokk:library:' + item_id + '>'

    try:
        with open('../blob.dokk.org/pdf_to_text/' + item_id + '.txt', 'r') as file:
            item_plaintext = file.read()
    except:
        item_plaintext = ''

    data = query(f'''
    PREFIX library: <dokk:library:>
    PREFIX license: <dokk:license:>

    CONSTRUCT {{
        ?item library:title ?title ;
              library:author ?author ;
              library:license ?license .

        ?license license:id ?license_id ;
                 license:name ?license_name .
    }}
    WHERE {{
        ?item
            library:title ?title ;
            library:author ?author ;
            library:license ?license .

        FILTER (?item = {item_iri})

        OPTIONAL {{
        ?license license:id ?license_id_optional ;
                 license:name ?license_name_optional .
        }}

        BIND(COALESCE(?license_id_optional, SUBSTR(STR(?license), 14)) AS ?license_id)
        BIND(COALESCE(?license_name_optional, SUBSTR(STR(?license), 14)) AS ?license_name)
    }}
    ''',
    {
        '@context': {
            'library': 'dokk:library:',
            'license': 'dokk:license:',
            'library:author': { '@container': '@set' },
            'library:license': { '@container': '@set' }
        },
        'library:title': {}
    })

    return template('library_item.html', data=data['@graph'][0], item_id=item_id, plaintext=item_plaintext)

@bottle.get('/license', name='licenses')
def licenses():
    """
    """

    data = query(
    '''
    PREFIX license: <dokk:license:>

    SELECT *
    WHERE {
        ?license license:id ?id ;
                 license:name ?name.
    }
    ORDER BY ASC(UCASE(?name))
    ''')

    return template('licenses.html', data=data)

@bottle.get('/manpages', name='manpages')
def manpages():
    """
    """

    data = query(
    '''
    PREFIX mp: <dokk:manpages:>

    SELECT DISTINCT ?distribution ?version
    WHERE {
        [] mp:source [
            mp:distribution_version ?version ;
            mp:distribution_name ?distribution ;
        ] .
    }
    ORDER BY ?distribution ?version
    ''')

    return template('manpages.html', data=data)

@bottle.get('/manpages/<distribution>/<version>', name='manpages_distribution')
def manpages_distribution(distribution, version):
    """
    List packages by distribution.
    """

    data = query(
    f'''
    PREFIX mp: <dokk:manpages:>

    SELECT DISTINCT ?package
    WHERE {{
        ?id mp:source [
                mp:distribution_name "{distribution}" ;
                mp:distribution_version "{version}" ;
                mp:package ?package ;
            ] .
    }}
    ORDER BY ?package
    ''')

    return template('manpages_distribution.html', data=data, distribution=distribution, version=version)

@bottle.get('/manpages/<distribution>/<version>/<package>', name='manpages_package')
def manpages_package(distribution, version, package):
    """
    List manpages by package.
    """

    data = query(
    f'''
    PREFIX mp: <dokk:manpages:>

    SELECT ?id ?filename
    WHERE {{
        ?id mp:source [
            mp:package "{package}" ;
            mp:distribution_version "{version}" ;
            mp:distribution_name "{distribution}" ;
            mp:filename ?filename ;
        ] .
    }}
    ORDER BY ?filename
    ''')

    return template('manpages_package.html', data=data, distribution=distribution, version=version, package=package)

@bottle.get('/manpages/<distribution>/<version>/<package>/<name>.<section>.<lang>', name='manpages_page')
def manpages_page(distribution, version, package, name, section, lang):
    """
    Display a single manpage.
    """

    page_id = f'<dokk:manpages:{distribution}/{version}/{package}/{name}.{section}.{lang}>'

    # 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: <dokk:manpages:>

    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)  ->  <a href="...">ls(1)</a>
    # regex explanation:
    #    - some manpages use a bold or italic name, so we match an optional <b>
    #      <i> tag
    #    - match valid characters for a manpage, assign name <page>
    #    - match optional closing </em> or </strong> tags
    #    - match '('
    #    - match number, assign name <section>
    #    - match ')'
    html = data['@graph'][0]['mp:html']
    html = re.sub (
        '(?:<b>|<i>)?(?P<page>[\w_.:-]+)(?:</b>|</i>)?\((?P<section>[0-9]\w*)\)',
        lambda match:
            f'''<a href="{application.get_url('manpages_disambiguation',
                          name=match.group('page'), section=match.group('section')).lower()}"
            >{match.group('page')}({match.group('section')})</a>''',
        html)

    data['@graph'][0]['mp:html'] = html

    return template('manpage.html', data=data['@graph'][0], distribution=distribution,
        version=version, package=package, name=name, section=section, lang=lang)

@bottle.get('/manpages/<name>.<section>', name='manpages_disambiguation')
def manpages_disambiguation(name, section):
    """
    Show a list of manpages that match <name> and <section>
    """

    data = query(
    f'''
    PREFIX mp: <dokk:manpages:>

    DESCRIBE ?page
    WHERE {{
        ?page mp:name_lowercase "{name}" ;
              mp:section_lowercase "{section}" .
    }}
    ''',
    {
        '@context': {
            'mp': 'dokk:manpages:',
        },
        'mp:source': {},
    })

    return template('manpage_disambiguation.html', name=name, section=section, data=data)

@bottle.get('/license/<id>', name='license')
def license(id):
    """
    """

    license_iri = 'license:' + id

    data = query(Template(
    '''
    PREFIX license: <dokk:license:>

    DESCRIBE $license
    ''').substitute(license=license_iri))

    return template('license.html', data=data)

@bottle.get('/mimi_and_eunice', name='mimi_and_eunice')
def mimi_and_eunice():
    """
    """

    data = query(
    '''
    PREFIX mimi_eunice: <dokk:mimi_and_eunice:>
    PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>

    SELECT ?number ?title
    WHERE {
        ?id mimi_eunice:title ?title .
        BIND (SUBSTR(STR(?id), 22) as ?number)
    }
    ORDER BY ?number
    ''')

    return template('mimi_and_eunice.html', data=data)

@bottle.get('/mimi_and_eunice/<number>', name='mimi_and_eunice_strip')
def mimi_and_eunice_strip(number):
    """
    """

    iri = '<dokk:mimi_and_eunice:' + number + '>'

    data = query(Template(
    '''
    PREFIX license: <dokk:license:>
    PREFIX mimi_eunice: <dokk:mimi_and_eunice:>

    DESCRIBE $iri ?license
    WHERE {
        $iri mimi_eunice:license ?license
    }
    ''').substitute(iri=iri),
    {
        '@context': {
            'license': 'dokk:license:',
            'mimi_eunice': 'dokk:mimi_and_eunice:',
            'mimi_eunice:tag': { '@container': '@set' },
            'mimi_eunice:transcript': { '@container': '@set' }
        },
        'mimi_eunice:license': {},
        'mimi_eunice:transcript': {
            'mimi_eunice:order': {}
        }
    })

    return template('mimi_and_eunice_strip.html', data=data['@graph'][0])

@bottle.get('/articles', name='articles')
def articles():
    """
    """

    data = query(
    '''
    PREFIX : <dokk:articles:>

    SELECT ?iri ?title
    WHERE {
        ?iri :title ?title
    }
    ORDER BY ?title
    ''')

    return template('articles.html', data=data)

@bottle.get('/<id>', name='article')
def article(id):
    """
    """

    data = query(
    f'''
    PREFIX : <dokk:articles:>

    DESCRIBE <dokk:articles:{id}>
    ''',
    {
        '@context': {
            'a': 'dokk:articles:'
        }
    })

    if '@graph' not in data or len(data['@graph']) < 1:
        bottle.abort(404, "Article doesn't exist.")

    return template('article.html', data=data['@graph'][0])