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

pass pandoc options to properties and rich text arrays #169

Merged
merged 4 commits into from
Feb 1, 2024
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
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,13 @@ Here are some features we're planning to add in the future:

## Changelog

### v0.9.1
- Delete print statement in `rich_text.py`

### v0.9.0
- pass the `pandoc_options` setting to wherever `pandoc.write` is called for consistency. Also,
this should reasonably be the expected behavior.

### v0.8.2

- stop filtering linebreaks from table cells
Expand Down
12 changes: 6 additions & 6 deletions n2y/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
def _page_properties(
page,
pandoc_format=None,
pandoc_options=None,
id_property=None,
url_property=None,
property_map=None,
Expand All @@ -26,7 +27,7 @@ def _page_properties(
pandoc_format = "markdown"
if property_map is None:
property_map = {}
properties = page.properties_to_values(pandoc_format)
properties = page.properties_to_values(pandoc_format, pandoc_options)
if id_property in properties:
logger.warning(
'The id property "%s" is shadowing an existing property with the same name',
Expand Down Expand Up @@ -77,6 +78,7 @@ def export_page(
page_properties = _page_properties(
page,
pandoc_format,
pandoc_options,
id_property,
url_property,
property_map,
Expand Down Expand Up @@ -123,16 +125,14 @@ def database_to_yaml(
):
if content_property in database.schema:
logger.warning(
(
'The content property "%s" is shadowing an existing '
"property with the same name"
),
'The content property "%s" is shadowing an existing '
"property with the same name",
content_property,
)
results = []
for page in database.children_filtered(notion_filter, notion_sorts):
result = _page_properties(
page, pandoc_format, id_property, url_property, property_map
page, pandoc_format, pandoc_options, id_property, url_property, property_map
)
if content_property:
pandoc_ast = page.to_pandoc()
Expand Down
7 changes: 5 additions & 2 deletions n2y/page.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,5 +117,8 @@ def to_pandoc(self, ignore_toc=False):
ast = self.block.to_pandoc()
return ast if ignore_toc else self.generate_toc(ast)

def properties_to_values(self, pandoc_format=None):
return {k: v.to_value(pandoc_format) for k, v in self.properties.items()}
def properties_to_values(self, pandoc_format=None, pandoc_options=None):
return {
k: v.to_value(pandoc_format, pandoc_options)
for k, v in self.properties.items()
}
3 changes: 1 addition & 2 deletions n2y/plugins/downloadfileproperty.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@
from os import makedirs, path
from urllib.parse import urlparse


from n2y.property_values import FilesPropertyValue
from n2y.utils import slugify


class DownloadFilePropertyValue(FilesPropertyValue):
def to_value(self, _):
def to_value(self, _, __):
url_list = []
for file in self.files:
file_content = self.client._get_url(file.url, stream=True)
Expand Down
46 changes: 23 additions & 23 deletions n2y/property_values.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def __init__(self, client, notion_data, page):
self.notion_type = notion_data["type"]
self.page = page

def to_value(self, pandoc_format=None):
def to_value(self, pandoc_format=None, pandoc_options=None):
raise NotImplementedError()


Expand All @@ -24,7 +24,7 @@ def __init__(self, client, notion_data, page):
super().__init__(client, notion_data, page)
self.rich_text = client.wrap_notion_rich_text_array(notion_data["title"])

def to_value(self, _=None):
def to_value(self, _=None, __=None):
# Notion allows styling of the title, however, in their UI they display
# the title property without any styling. Thus, if you copy/paste styled
# text into a title this styling can be hidden and can re-appear after
Expand All @@ -40,19 +40,19 @@ def __init__(self, client, notion_data, page):
super().__init__(client, notion_data, page)
self.rich_text = client.wrap_notion_rich_text_array(notion_data["rich_text"])

def to_value(self, pandoc_format="gfm"):
def to_value(self, pandoc_format="gfm", pandoc_options=None):
if pandoc_format is None:
return self.rich_text.to_plain_text()
else:
return self.rich_text.to_value(pandoc_format)
return self.rich_text.to_value(pandoc_format, pandoc_options)


class NumberPropertyValue(PropertyValue):
def __init__(self, client, notion_data, page):
super().__init__(client, notion_data, page)
self.number = notion_data["number"]

def to_value(self, _=None):
def to_value(self, _=None, __=None):
return self.number


Expand All @@ -61,7 +61,7 @@ def __init__(self, client, notion_data, page):
super().__init__(client, notion_data, page)
self.status = notion_data["status"]

def to_value(self, _=None):
def to_value(self, _=None, __=None):
# Note: the Notion UI shouldn't allow you to have two statuses with the
# same name nor should it allow no status at all
return self.status
Expand All @@ -80,7 +80,7 @@ def __init__(self, client, notion_data, page):
self.name = None
self.color = None

def to_value(self, _=None):
def to_value(self, _=None, __=None):
# Note: the Notion UI shouldn't allow you to have two options with the
# same name
return self.name
Expand All @@ -93,7 +93,7 @@ def __init__(self, client, notion_data, page):
MultiSelectOption(self.client, no) for no in notion_data["multi_select"]
]

def to_value(self, _=None):
def to_value(self, _=None, __=None):
# Note: the Notion UI shouldn't allow you to have two options with the
# same name
return [o.name for o in self.options]
Expand All @@ -113,7 +113,7 @@ def __init__(self, client, notion_data, page):
super().__init__(client, notion_data, page)
self.value = process_notion_date(notion_data["date"])

def to_value(self, _=None):
def to_value(self, _=None, __=None):
return self.value

def to_plain_text(self):
Expand All @@ -125,7 +125,7 @@ def __init__(self, client, notion_data, page):
super().__init__(client, notion_data, page)
self.people = [client.wrap_notion_user(nu) for nu in notion_data["people"]]

def to_value(self, _=None):
def to_value(self, _=None, __=None):
return [u.to_value() for u in self.people]


Expand All @@ -134,7 +134,7 @@ def __init__(self, client, notion_data, page):
super().__init__(client, notion_data, page)
self.files = [client.wrap_notion_file(nf) for nf in notion_data["files"]]

def to_value(self, _):
def to_value(self, _, __):
return [f.to_value() for f in self.files]


Expand All @@ -143,7 +143,7 @@ def __init__(self, client, notion_data, page):
super().__init__(client, notion_data, page)
self.checkbox = notion_data["checkbox"]

def to_value(self, _):
def to_value(self, _, __):
return self.checkbox


Expand All @@ -152,7 +152,7 @@ def __init__(self, client, notion_data, page):
super().__init__(client, notion_data, page)
self.url = notion_data["url"]

def to_value(self, _=None):
def to_value(self, _=None, __=None):
return self.url


Expand All @@ -161,7 +161,7 @@ def __init__(self, client, notion_data, page):
super().__init__(client, notion_data, page)
self.email = notion_data["email"]

def to_value(self, _=None):
def to_value(self, _=None, __=None):
return self.email


Expand All @@ -170,7 +170,7 @@ def __init__(self, client, notion_data, page):
super().__init__(client, notion_data, page)
self.phone_number = notion_data["phone_number"]

def to_value(self, _=None):
def to_value(self, _=None, __=None):
return self.phone_number


Expand All @@ -184,7 +184,7 @@ def __init__(self, client, notion_data, page):
else:
self.value = notion_formula[notion_formula["type"]]

def to_value(self, _=None):
def to_value(self, _=None, __=None):
return self.value


Expand All @@ -200,7 +200,7 @@ def __init__(self, client, notion_data, page):
for r in client._paginated_request(client._get_url, url, {})
]

def to_value(self, _=None):
def to_value(self, _=None, __=None):
return self.ids


Expand Down Expand Up @@ -228,15 +228,15 @@ def __init__(self, client, notion_data, page):
self.value = notion_rollup[notion_rollup["type"]]
# TODO: handle arrays of dates

def to_value(self, pandoc_format="gfm"):
def to_value(self, pandoc_format="gfm", pandoc_options=None):
if self.rollup_type == "date":
return self.value
elif self.rollup_type == "string":
return self.value
elif self.rollup_type == "number":
return self.value
elif self.rollup_type == "array":
return [pv.to_value(pandoc_format) for pv in self.value]
return [pv.to_value(pandoc_format, pandoc_options) for pv in self.value]
else:
return self.value

Expand All @@ -246,7 +246,7 @@ def __init__(self, client, notion_data, page):
super().__init__(client, notion_data, page)
self.created_time = fromisoformat(notion_data["created_time"])

def to_value(self, _=None):
def to_value(self, _=None, __=None):
return datetime.isoformat(self.created_time)


Expand All @@ -255,7 +255,7 @@ def __init__(self, client, notion_data, page):
super().__init__(client, notion_data, page)
self.created_by = client.wrap_notion_user(notion_data["created_by"])

def to_value(self, _=None):
def to_value(self, _=None, __=None):
return self.created_by.to_value()


Expand All @@ -264,7 +264,7 @@ def __init__(self, client, notion_data, page):
super().__init__(client, notion_data, page)
self.last_edited_time = fromisoformat(notion_data["last_edited_time"])

def to_value(self, _=None):
def to_value(self, _=None, __=None):
return datetime.isoformat(self.last_edited_time)


Expand All @@ -273,7 +273,7 @@ def __init__(self, client, notion_data, page):
super().__init__(client, notion_data, page)
self.last_edited_by = client.wrap_notion_user(notion_data["last_edited_by"])

def to_value(self, _=None):
def to_value(self, _=None, __=None):
return self.last_edited_by.to_value()


Expand Down
24 changes: 12 additions & 12 deletions n2y/rich_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@
from collections import deque

from pandoc.types import (
Str,
Space,
LineBreak,
Strong,
Emph,
Strikeout,
Code,
Emph,
InlineMath,
LineBreak,
Link,
Underline,
Math,
InlineMath,
Space,
Str,
Strikeout,
Strong,
Underline,
)

from n2y.logger import logger
Expand Down Expand Up @@ -49,11 +49,11 @@ def __init__(self, client, notion_data, block=None):
def to_pandoc(self):
raise NotImplementedError()

def to_value(self, pandoc_format):
def to_value(self, pandoc_format, pandoc_options):
return pandoc_write_or_log_errors(
self.to_pandoc(),
format=pandoc_format,
options=[],
options=pandoc_options,
)

@classmethod
Expand Down Expand Up @@ -211,11 +211,11 @@ def __getitem__(self, index):
def to_pandoc(self):
return sum([item.to_pandoc() for item in self.items], [])

def to_value(self, pandoc_format):
def to_value(self, pandoc_format, pandoc_options):
return pandoc_write_or_log_errors(
self.to_pandoc(),
format=pandoc_format,
options=[],
options=pandoc_options,
)

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

setup(
name="n2y",
version="0.8.2",
version="0.9.1",
description=description,
long_description=description,
long_description_content_type="text/x-rst",
Expand Down
2 changes: 1 addition & 1 deletion tests/test_property_values.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def process_property_value(notion_data, wrap_notion_user):
else:
wrap_notion_user.return_value = User(client, mock_user())
property_value = client.wrap_notion_property_value(notion_data, None)
return property_value.to_value("gfm")
return property_value.to_value("gfm", [])


def test_title():
Expand Down
Loading
Loading