-
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.
- Loading branch information
1 parent
a748e23
commit 81092a6
Showing
18 changed files
with
101 additions
and
30 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
from bs4 import BeautifulSoup | ||
from marker.v2.renderers import BaseRenderer | ||
from marker.v2.schema import BlockTypes | ||
|
||
|
||
class HTMLRenderer(BaseRenderer): | ||
remove_blocks: list = [BlockTypes.PageHeader, BlockTypes.PageFooter] | ||
def extract_html(self, document_output): | ||
soup = BeautifulSoup(document_output.html, 'html.parser') | ||
|
||
content_refs = soup.find_all('content-ref') | ||
ref_block_type = None | ||
for ref in content_refs: | ||
src = ref.get('src') | ||
for item in document_output.children: | ||
if item.id == src: | ||
content = self.extract_html(item) | ||
ref_block_type = item.id.block_type | ||
break | ||
|
||
if ref_block_type in self.remove_blocks: | ||
ref.replace_with('') | ||
else: | ||
ref.replace_with(BeautifulSoup(f"<div>{content}</div>", 'html.parser')) | ||
|
||
return str(soup) | ||
|
||
def __call__(self, document_output): | ||
full_html = self.extract_html(document_output) | ||
return full_html |
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,26 +1,15 @@ | ||
from bs4 import BeautifulSoup | ||
from markdownify import markdownify | ||
from marker.v2.renderers import BaseRenderer | ||
|
||
|
||
class MarkdownRenderer(BaseRenderer): | ||
def extract_html(self, document_output): | ||
soup = BeautifulSoup(document_output.html, 'html.parser') | ||
|
||
content_refs = soup.find_all('content-ref') | ||
for ref in content_refs: | ||
src = ref.get('src') | ||
for item in document_output.children: | ||
if item.id == src: | ||
content = self.extract_html(item) | ||
break | ||
|
||
ref.replace_with(BeautifulSoup(content, 'html.parser')) | ||
|
||
return str(soup) | ||
from marker.v2.renderers.html import HTMLRenderer | ||
|
||
class MarkdownRenderer(HTMLRenderer): | ||
def __call__(self, document_output): | ||
full_html = self.extract_html(document_output) | ||
return markdownify(full_html) | ||
return markdownify( | ||
full_html, | ||
heading_style="ATX", | ||
bullets="-", | ||
escape_misc=False, | ||
escape_underscores=False | ||
) | ||
|
||
|
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
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,9 +1,19 @@ | ||
import re | ||
|
||
from marker.v2.schema.blocks import Block | ||
|
||
|
||
def replace_bullets(text): | ||
# Replace bullet characters with a - | ||
bullet_pattern = r"(^|[\n ])[•●○■▪▫–—]( )" | ||
replaced_string = re.sub(bullet_pattern, r"\1-\2", text) | ||
return replaced_string | ||
|
||
class ListItem(Block): | ||
block_type: str = "ListItem" | ||
|
||
def assemble_html(self, child_blocks, parent_structure): | ||
template = super().assemble_html(child_blocks, parent_structure) | ||
template = template.replace("\n", " ") | ||
template = replace_bullets(template) | ||
return f"<li>{template}</li>" |
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 |
---|---|---|
@@ -1,10 +1,14 @@ | ||
from typing import List | ||
|
||
from tabled.formats import html_format | ||
from tabled.schema import SpanTableCell | ||
|
||
from marker.v2.schema.blocks import Block | ||
|
||
|
||
class Table(Block): | ||
block_type: str = "Table" | ||
cells: List[SpanTableCell] | None = None | ||
cells: List[SpanTableCell] | None = None | ||
|
||
def assemble_html(self, child_blocks, parent_structure=None): | ||
return html_format(self.cells) |
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,5 +1,4 @@ | ||
from marker.v2.schema.blocks import Block | ||
|
||
|
||
class TableGroup(Block): | ||
block_type: str = "TableGroup" |
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