|
31 | 31 | from numbers import Integral, Real |
32 | 32 | from frozendict import frozendict |
33 | 33 | from pyld.__about__ import (__copyright__, __license__, __version__) |
| 34 | +from .iri_resolver import resolve |
34 | 35 |
|
35 | 36 | def cmp(a, b): |
36 | 37 | return (a > b) - (a < b) |
@@ -443,80 +444,6 @@ def unregister_rdf_parser(content_type): |
443 | 444 | del _rdf_parsers[content_type] |
444 | 445 |
|
445 | 446 |
|
446 | | -def prepend_base(base, iri): |
447 | | - """ |
448 | | - Prepends a base IRI to the given relative IRI. |
449 | | -
|
450 | | - :param base: the base IRI. |
451 | | - :param iri: the relative IRI. |
452 | | -
|
453 | | - :return: the absolute IRI. |
454 | | - """ |
455 | | - # skip IRI processing |
456 | | - if base is None: |
457 | | - return iri |
458 | | - |
459 | | - # already an absolute iri |
460 | | - if _is_absolute_iri(iri): |
461 | | - return iri |
462 | | - |
463 | | - # parse IRIs |
464 | | - base = parse_url(base) |
465 | | - rel = parse_url(iri) |
466 | | - |
467 | | - # per RFC3986 5.2.2 |
468 | | - transform = { |
469 | | - 'scheme': base.scheme |
470 | | - } |
471 | | - |
472 | | - if rel.authority is not None: |
473 | | - transform['authority'] = rel.authority |
474 | | - transform['path'] = rel.path |
475 | | - transform['query'] = rel.query |
476 | | - else: |
477 | | - transform['authority'] = base.authority |
478 | | - |
479 | | - if rel.path == '': |
480 | | - transform['path'] = base.path |
481 | | - if rel.query is not None: |
482 | | - transform['query'] = rel.query |
483 | | - else: |
484 | | - transform['query'] = base.query |
485 | | - else: |
486 | | - if rel.path.startswith('/'): |
487 | | - # IRI represents an absolute path |
488 | | - transform['path'] = rel.path |
489 | | - else: |
490 | | - # merge paths |
491 | | - path = base.path |
492 | | - |
493 | | - # append relative path to the end of the last directory from |
494 | | - # base |
495 | | - path = path[0:path.rfind('/') + 1] |
496 | | - if (len(path) > 0 or base.authority) and not path.endswith('/'): |
497 | | - path += '/' |
498 | | - path += rel.path |
499 | | - |
500 | | - transform['path'] = path |
501 | | - |
502 | | - transform['query'] = rel.query |
503 | | - |
504 | | - if rel.path != '': |
505 | | - # normalize path |
506 | | - transform['path'] = remove_dot_segments(transform['path']) |
507 | | - |
508 | | - transform['fragment'] = rel.fragment |
509 | | - |
510 | | - # construct URL |
511 | | - rval = unparse_url(transform) |
512 | | - |
513 | | - # handle empty base case |
514 | | - if rval == '': |
515 | | - rval = './' |
516 | | - |
517 | | - return rval |
518 | | - |
519 | | - |
520 | 447 | def remove_base(base, iri): |
521 | 448 | """ |
522 | 449 | Removes a base IRI from the given absolute IRI. |
@@ -3188,10 +3115,10 @@ def _process_context(self, active_ctx, local_ctx, options, |
3188 | 3115 | base = ctx['@base'] |
3189 | 3116 | if base is None: |
3190 | 3117 | base = None |
3191 | | - elif _is_absolute_iri(base): |
| 3118 | + elif _is_absolute_iri(base) or (_is_relative_iri(base) and active_ctx.get('@base') is None): |
3192 | 3119 | base = base |
3193 | | - elif _is_relative_iri(base): |
3194 | | - base = prepend_base(active_ctx.get('@base'), base) |
| 3120 | + elif _is_relative_iri(base) and active_ctx.get('@base') is not None: |
| 3121 | + base = resolve(base, active_ctx.get('@base')) |
3195 | 3122 | else: |
3196 | 3123 | raise JsonLdError( |
3197 | 3124 | 'Invalid JSON-LD syntax; the value of "@base" in a ' |
@@ -3295,7 +3222,7 @@ def _process_context(self, active_ctx, local_ctx, options, |
3295 | 3222 | process = True |
3296 | 3223 |
|
3297 | 3224 | if _is_string(key_ctx): |
3298 | | - url = prepend_base(options['base'], key_ctx) |
| 3225 | + url = resolve(key_ctx, options['base']) |
3299 | 3226 | if url in cycles: |
3300 | 3227 | process = False |
3301 | 3228 | else: |
@@ -4887,7 +4814,7 @@ def _compact_iri( |
4887 | 4814 | if active_ctx['@base'] is None: |
4888 | 4815 | return iri |
4889 | 4816 | else: |
4890 | | - return remove_base(prepend_base(base, active_ctx['@base']), iri) |
| 4817 | + return remove_base(resolve(active_ctx['@base'], base), iri) |
4891 | 4818 | else: |
4892 | 4819 | return remove_base(base, iri) |
4893 | 4820 |
|
@@ -5510,9 +5437,10 @@ def _expand_iri( |
5510 | 5437 | if base and '@base' in active_ctx: |
5511 | 5438 | # The None case preserves rval as potentially relative |
5512 | 5439 | if active_ctx['@base'] is not None: |
5513 | | - rval = prepend_base(prepend_base(base, active_ctx['@base']), rval) |
| 5440 | + resolved_base = active_ctx['@base'] if _is_absolute_iri(active_ctx['@base']) else resolve(active_ctx['@base'], base) |
| 5441 | + rval = resolve(rval, resolved_base) |
5514 | 5442 | elif base: |
5515 | | - rval = prepend_base(base, rval) |
| 5443 | + rval = resolve(rval, base) |
5516 | 5444 |
|
5517 | 5445 | return rval |
5518 | 5446 |
|
@@ -6642,7 +6570,7 @@ def load_html(input, url, profile, options): |
6642 | 6570 | # use either specified base, or document location |
6643 | 6571 | effective_base = options.get('base', url) |
6644 | 6572 | if effective_base: |
6645 | | - html_base = prepend_base(effective_base, html_base[0]) |
| 6573 | + html_base = resolve(html_base[0], effective_base) |
6646 | 6574 | options['base'] = html_base |
6647 | 6575 |
|
6648 | 6576 | url_elements = parse_url(url) |
|
0 commit comments