Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

filter LineBreak class from table cells #93

Merged
merged 4 commits into from
Dec 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,10 @@ Here are some features we're planning to add in the future:

## Changelog

### v0.7.3

- Fix bug by filtering LineBreak class from table cells.

### v0.7.2

- Fix bug in mermaid by fixing error image file path.
Expand Down
2 changes: 1 addition & 1 deletion n2y/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ class RowBlock(Block):
def __init__(self, client, notion_data, page, get_children=True):
super().__init__(client, notion_data, page, get_children)
self.cells = [
client.wrap_notion_rich_text_array(nc, self)
client.wrap_notion_rich_text_array(nc, self, True)
for nc in self.notion_type_data["cells"]
]

Expand Down
10 changes: 6 additions & 4 deletions n2y/notion.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,11 +201,13 @@ def wrap_notion_file(self, notion_data):
def wrap_notion_emoji(self, notion_data):
return self.instantiate_class("emoji", None, self, notion_data)

def wrap_notion_rich_text_array(self, notion_data, block=None):
return self.instantiate_class("rich_text_array", None, self, notion_data, block)
def wrap_notion_rich_text_array(self, notion_data, block=None, table=None):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like table is a boolean everywhere else. Should we set a default boolean value rather than None here? Same thing below.

return self.instantiate_class("rich_text_array", None, self, notion_data, block, table)

def wrap_notion_rich_text(self, notion_data, block=None):
return self.instantiate_class("rich_texts", notion_data["type"], self, notion_data, block)
def wrap_notion_rich_text(self, notion_data, block=None, table=None):
return self.instantiate_class(
"rich_texts", notion_data["type"], self, notion_data, block, table
)

def wrap_notion_mention(self, notion_data, plain_text, block=None):
# here we pass in the plain_text to avoid the need to query the page
Expand Down
4 changes: 2 additions & 2 deletions n2y/plugins/footnotes.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ def _footnote_empty(self):


class TextRichTextWithFootnoteRef(TextRichText):
def __init__(self, client, notion_data, block=None):
super().__init__(client, notion_data, block)
def __init__(self, client, notion_data, block=None, table=None):
super().__init__(client, notion_data, block, table)
if not self._is_footnote():
raise UseNextClass()

Expand Down
33 changes: 18 additions & 15 deletions n2y/rich_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,15 @@ class RichText:
a property_value or somewhere else.
"""

def __init__(self, client, notion_data, block=None):
def __init__(self, client, notion_data, block=None, table=False):
self.client = client
self.block = block
self.table = table

self.plain_text = notion_data['plain_text']
self.href = notion_data.get('href', None)
self.notion_type = notion_data['type']

self.notion_data = notion_data
annotations = notion_data['annotations']
self.bold = annotations["bold"]
self.italic = annotations["italic"]
Expand All @@ -47,10 +48,9 @@ def to_markdown(self):
return pandoc_ast_to_markdown(self.to_pandoc()).strip('\n')

@classmethod
def plain_text_to_pandoc(klass, plain_text):
def plain_text_to_pandoc(klass, plain_text, table=False):
ast = []
match = re.findall(r"( +)|(\xa0+)|(\S+)|(\n+)|(\t+)", plain_text)

for m in match:
space, non_breaking_space, word, newline, tab = m
for _ in range(len(space)):
Expand All @@ -60,7 +60,10 @@ def plain_text_to_pandoc(klass, plain_text):
if word:
ast.append(Str(word))
for _ in range(len(newline)):
ast.append(LineBreak())
if table:
ast.append(Space())
else:
ast.append(LineBreak())
for _ in range(len(tab) * 4): # 4 spaces per tab
ast.append(Space())
return ast
Expand Down Expand Up @@ -109,8 +112,8 @@ def annotate_pandoc_ast(self, target):


class MentionRichText(RichText):
def __init__(self, client, notion_data, block=None):
super().__init__(client, notion_data, block)
def __init__(self, client, notion_data, block=None, table=False):
super().__init__(client, notion_data, block, table)
self.mention = client.wrap_notion_mention(
notion_data['mention'], notion_data["plain_text"], block,
)
Expand All @@ -123,8 +126,8 @@ def to_pandoc(self):


class EquationRichText(RichText):
def __init__(self, client, notion_data, block=None):
super().__init__(client, notion_data, block)
def __init__(self, client, notion_data, block=None, table=False):
super().__init__(client, notion_data, block, table)
self.expression = notion_data['equation']['expression']

def to_pandoc(self):
Expand All @@ -140,17 +143,16 @@ def from_plain_text(klass, client, string, block=None):
notion_data = mock_rich_text(string)
return klass(client, notion_data, block)

def __init__(self, client, notion_data, block=None):
super().__init__(client, notion_data, block)
def __init__(self, client, notion_data, block=None, table=False):
super().__init__(client, notion_data, block, table)

def to_pandoc(self):
if not self.code:
plain_text_ast = self.plain_text_to_pandoc(self.plain_text)
plain_text_ast = self.plain_text_to_pandoc(self.plain_text, self.table)
annotated_ast = self.annotate_pandoc_ast(plain_text_ast)
else:
code_ast = [Code(("", [], []), self.plain_text)]
annotated_ast = self.annotate_pandoc_ast(code_ast)

if self.href:
return [Link(
('', [], []),
Expand Down Expand Up @@ -181,12 +183,13 @@ class RichTextArray:
be bolded, and the third would contain " the hot dog.".
"""

def __init__(self, client, notion_data, block=None):
def __init__(self, client, notion_data, block=None, table=None):
self.client = client
self.block = block
self.table = table

assert isinstance(notion_data, list)
self.items = [client.wrap_notion_rich_text(i, block) for i in notion_data]
self.items = [client.wrap_notion_rich_text(i, block, table) for i in notion_data]

def __len__(self):
return len(self.items)
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

setup(
name='n2y',
version='0.7.2',
version='0.7.3',
description=description,
long_description=description,
long_description_content_type='text/x-rst',
Expand Down
5 changes: 5 additions & 0 deletions tests/test_plain_text_to_pandoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,8 @@ def test_plain_text_to_pandoc_spaces_after():
def test_plain_text_to_pandoc_spaces_after_newline():
pandoc_ast = RichText.plain_text_to_pandoc("hello\n world")
assert pandoc_ast == [Str("hello"), LineBreak(), Space(), Space(), Str("world")]


def test_plain_text_to_pandoc_table():
pandoc_ast = RichText.plain_text_to_pandoc("hello\n world", True)
assert pandoc_ast == [Str("hello"), Space(), Space(), Space(), Str("world")]