-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #357 from VikParuchuri/vik_v2
Add simple line and span renderer, add blocktype class
- Loading branch information
Showing
20 changed files
with
129 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import re | ||
from typing import List, Optional | ||
|
||
from marker.v2.renderers import BaseRenderer | ||
from marker.v2.schema import BlockTypes | ||
from marker.v2.schema.text import Span | ||
|
||
|
||
def surround_text(s, char_to_insert): | ||
leading_whitespace = re.match(r'^(\s*)', s).group(1) | ||
trailing_whitespace = re.search(r'(\s*)$', s).group(1) | ||
stripped_string = s.strip() | ||
modified_string = char_to_insert + stripped_string + char_to_insert | ||
final_string = leading_whitespace + modified_string + trailing_whitespace | ||
return final_string | ||
|
||
|
||
class LineRenderer(BaseRenderer): | ||
block_type = BlockTypes.Line | ||
|
||
def __call__(self, document, block, children: Optional[List[Span]] = None): | ||
text = "" | ||
for i, child in enumerate(children): | ||
next_span = None | ||
next_idx = i + 1 | ||
while len(children) > next_idx: | ||
next_span = children[next_idx] | ||
next_idx += 1 | ||
if len(next_span.text.strip()) > 0: | ||
break | ||
span_text = child.rendered | ||
|
||
# Don't bold or italicize very short sequences | ||
# Avoid bolding first and last sequence so lines can be joined properly | ||
if len(span_text) > 3 and 0 < i < len(children) - 1: | ||
if child.italic and (not next_span or not next_span.italic): | ||
span_text = surround_text(span_text, "*") | ||
elif child.bold and (not next_span or not next_span.bold): | ||
span_text = surround_text(span_text, "**") | ||
text += span_text | ||
return text |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from marker.v2.renderers import BaseRenderer | ||
from marker.v2.schema import BlockTypes | ||
|
||
|
||
class SpanRenderer(BaseRenderer): | ||
block_type = BlockTypes.Span | ||
|
||
def __call__(self, document, block, children=None): | ||
return block.text |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,18 @@ | ||
from marker.v2.schema.blocks import Block, LAYOUT_BLOCK_REGISTRY | ||
from marker.v2.schema.groups import GROUP_BLOCK_REGISTRY | ||
from marker.v2.schema.text import TEXT_BLOCK_REGISTRY | ||
|
||
|
||
class _BlockTypes: | ||
def __init__(self): | ||
pass | ||
|
||
def add(self, registry: dict[str, Block]): | ||
for k, v in registry.items(): | ||
setattr(self, k, k) | ||
|
||
|
||
BlockTypes = _BlockTypes() | ||
BlockTypes.add(GROUP_BLOCK_REGISTRY) | ||
BlockTypes.add(TEXT_BLOCK_REGISTRY) | ||
BlockTypes.add(LAYOUT_BLOCK_REGISTRY) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,4 @@ | |
|
||
|
||
class InlineMath(Block): | ||
block_type: str = "Text-inline-math" | ||
block_type: str = "TextInlineMath" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,4 @@ | |
|
||
|
||
class ListItem(Block): | ||
block_type: str = "List-item" | ||
block_type: str = "ListItem" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,4 @@ | |
|
||
|
||
class PageFooter(Block): | ||
block_type: str = "Page-footer" | ||
block_type: str = "PageFooter" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,4 @@ | |
|
||
|
||
class PageHeader(Block): | ||
block_type: str = "Page-header" | ||
block_type: str = "PageHeader" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,4 @@ | |
|
||
|
||
class SectionHeader(Block): | ||
block_type: str = "Section-header" | ||
block_type: str = "SectionHeader" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,4 @@ | |
|
||
|
||
class TableOfContents(Block): | ||
block_type: str = "Table-of-contents" | ||
block_type: str = "TableOfContents" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from marker.v2.schema.text.line import Line | ||
from marker.v2.schema.text.span import Span | ||
|
||
|
||
TEXT_BLOCK_REGISTRY = { | ||
"Line": Line, | ||
"Span": Span, | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,3 +6,6 @@ | |
|
||
class Line(Block): | ||
block_type: str = "Line" | ||
|
||
def is_continuation(self, other): | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters