populate the match field in d by escaping m.match and generating links to any command/process substitutions
(d, m, expansions, explain_prefix="/explain")
| 576 | |
| 577 | |
| 578 | def format_match(d, m, expansions, explain_prefix="/explain"): |
| 579 | """populate the match field in d by escaping m.match and generating |
| 580 | links to any command/process substitutions""" |
| 581 | |
| 582 | # save us some work later: do any expansions overlap |
| 583 | # the current match? |
| 584 | has_subs_in_match = False |
| 585 | |
| 586 | for start, end, kind in expansions: |
| 587 | if m.start <= start and end <= m.end: |
| 588 | has_subs_in_match = True |
| 589 | break |
| 590 | |
| 591 | # if not, just escape the current match |
| 592 | if not has_subs_in_match: |
| 593 | d["match"] = markupsafe.escape(m.match) |
| 594 | return |
| 595 | |
| 596 | # used in es.js |
| 597 | d["commandclass"] += " hasexpansion" |
| 598 | |
| 599 | # go over the expansions, wrapping them with a link; leave everything else |
| 600 | # untouched |
| 601 | expanded_match = "" |
| 602 | i = 0 |
| 603 | for start, end, kind in expansions: |
| 604 | if start >= m.end: |
| 605 | break |
| 606 | rel_start = start - m.start |
| 607 | rel_end = end - m.start |
| 608 | |
| 609 | if i < rel_start: |
| 610 | for j in range(i, rel_start): |
| 611 | if m.match[j].isspace(): |
| 612 | expanded_match += markupsafe.Markup(" ") |
| 613 | else: |
| 614 | expanded_match += markupsafe.escape(m.match[j]) |
| 615 | i = rel_start + 1 |
| 616 | if m.start <= start and end <= m.end: |
| 617 | s = m.match[rel_start:rel_end] |
| 618 | |
| 619 | if kind == "substitution": |
| 620 | content = markupsafe.Markup(_substitution_markup(s, explain_prefix)) |
| 621 | else: |
| 622 | content = s |
| 623 | |
| 624 | expanded_match += markupsafe.Markup( |
| 625 | '<span class="expansion-{0}">{1}</span>' |
| 626 | ).format(kind, content) |
| 627 | i = rel_end |
| 628 | |
| 629 | if i < len(m.match): |
| 630 | expanded_match += markupsafe.escape(m.match[i:]) |
| 631 | |
| 632 | assert expanded_match |
| 633 | d["match"] = expanded_match |
| 634 | |
| 635 |
no test coverage detected