Emphasis processor for handling strong and em matches inside asterisks.
| 541 | |
| 542 | |
| 543 | class AsteriskProcessor(InlineProcessor): |
| 544 | """Emphasis processor for handling strong and em matches inside asterisks.""" |
| 545 | |
| 546 | PATTERNS = [ |
| 547 | EmStrongItem(re.compile(EM_STRONG_RE, re.DOTALL | re.UNICODE), 'double', 'strong,em'), |
| 548 | EmStrongItem(re.compile(STRONG_EM_RE, re.DOTALL | re.UNICODE), 'double', 'em,strong'), |
| 549 | EmStrongItem(re.compile(STRONG_EM3_RE, re.DOTALL | re.UNICODE), 'double2', 'strong,em'), |
| 550 | EmStrongItem(re.compile(STRONG_RE, re.DOTALL | re.UNICODE), 'single', 'strong'), |
| 551 | EmStrongItem(re.compile(EMPHASIS_RE, re.DOTALL | re.UNICODE), 'single', 'em') |
| 552 | ] |
| 553 | """ The various strong and emphasis patterns handled by this processor. """ |
| 554 | |
| 555 | def build_single(self, m: re.Match[str], tag: str, idx: int) -> etree.Element: |
| 556 | """Return single tag.""" |
| 557 | el1 = etree.Element(tag) |
| 558 | text = m.group(2) |
| 559 | self.parse_sub_patterns(text, el1, None, idx) |
| 560 | return el1 |
| 561 | |
| 562 | def build_double(self, m: re.Match[str], tags: str, idx: int) -> etree.Element: |
| 563 | """Return double tag.""" |
| 564 | |
| 565 | tag1, tag2 = tags.split(",") |
| 566 | el1 = etree.Element(tag1) |
| 567 | el2 = etree.Element(tag2) |
| 568 | text = m.group(2) |
| 569 | self.parse_sub_patterns(text, el2, None, idx) |
| 570 | el1.append(el2) |
| 571 | if len(m.groups()) == 3: |
| 572 | text = m.group(3) |
| 573 | self.parse_sub_patterns(text, el1, el2, idx) |
| 574 | return el1 |
| 575 | |
| 576 | def build_double2(self, m: re.Match[str], tags: str, idx: int) -> etree.Element: |
| 577 | """Return double tags (variant 2): `<strong>text <em>text</em></strong>`.""" |
| 578 | |
| 579 | tag1, tag2 = tags.split(",") |
| 580 | el1 = etree.Element(tag1) |
| 581 | el2 = etree.Element(tag2) |
| 582 | text = m.group(2) |
| 583 | self.parse_sub_patterns(text, el1, None, idx) |
| 584 | text = m.group(3) |
| 585 | el1.append(el2) |
| 586 | self.parse_sub_patterns(text, el2, None, idx) |
| 587 | return el1 |
| 588 | |
| 589 | def parse_sub_patterns( |
| 590 | self, data: str, parent: etree.Element, last: etree.Element | None, idx: int |
| 591 | ) -> None: |
| 592 | """ |
| 593 | Parses sub patterns. |
| 594 | |
| 595 | `data`: text to evaluate. |
| 596 | |
| 597 | `parent`: Parent to attach text and sub elements to. |
| 598 | |
| 599 | `last`: Last appended child to parent. Can also be None if parent has no children. |
| 600 |
no test coverage detected