Skip to content

Commit

Permalink
Support Text Supply
Browse files Browse the repository at this point in the history
  • Loading branch information
hughcameron committed Aug 5, 2023
1 parent 386f6cb commit f2db997
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 15 deletions.
34 changes: 21 additions & 13 deletions markline/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
from builtins import slice
from collections import Counter
from datetime import datetime
from html import unescape
from select import select
from typing import Callable, List, NamedTuple, Union

import httpx
Expand Down Expand Up @@ -77,7 +75,7 @@ class CSSLocator(NamedTuple):


def loc(
name_selector: str,
name_selector: str = None,
attrs: dict = {},
recursive: bool = True,
namespaces: dict = {},
Expand Down Expand Up @@ -417,23 +415,28 @@ def __init__(
self,
url: str = None,
filepath: str = None,
text: str = None,
parser: str = "lxml",
unshorten: bool = True,
trim: bool = True,
client: httpx.Client = httpx.Client(),
meta_arrays: list = [],
):
if not any((url, filepath)):
raise ValueError("One of URL or filepath must be provided.")
if all((url, filepath)):
raise ValueError("Only one of URL or filepath can be provided.")
if not any((url, filepath, text)):
raise ValueError("One of URL, filepath or text must be provided.")
if all((url, filepath, text)):
raise ValueError("Only one of URL, filepath or text can be provided.")
if text:
self.url = "text_supplied"
if filepath:
self.url = filepath
if url:
self.client = client
url = prepare_url(url, unshorten, trim, self.client)
self.url = url
self.original = self.fetch_content(url=url, parser=parser, filepath=filepath)
self.original = self.fetch_content(
url=url, parser=parser, filepath=filepath, text=text
)
self.draft = copy.copy(self.original)
self.meta_arrays = meta_arrays
self.meta = self.gather_meta()
Expand All @@ -444,6 +447,7 @@ def fetch_content(
url: str,
parser: str = "lxml",
filepath: str = None,
text: str = None,
) -> BeautifulSoup:
"""Fetch the HTML content of a URL.
Content is fetched from the URL or local file and parsed as
Expand All @@ -464,6 +468,8 @@ def fetch_content(
"""
if parser == "lxml" and not package_available("lxml"):
parser = "html.parser"
if text:
return BeautifulSoup(text, parser)
if filepath:
with open(filepath, "r") as f:
return BeautifulSoup(f.read(), parser)
Expand Down Expand Up @@ -765,7 +771,7 @@ def to_html(self, filepath: str = None) -> str:
return f.write(self.draft.prettify())
return self.draft.prettify()

def to_md(self, filepath: str = None, outliner: str = None) -> str:
def to_md(self, filepath: str = None, outliner: str = None, **kwargs) -> str:
"""Render the draft as Markdown.
Accepts the default input and output formats for the render() method.
Expand All @@ -775,13 +781,15 @@ def to_md(self, filepath: str = None, outliner: str = None) -> str:
to generate an block outline suitable for use in Logseq. Currently only the
`newlines` and `paragraphs` outliners are supported.
Returns:
str: Markdown content.
Args:
filepath (str, optional): Filepath to write HTML content to.
Defaults to None.
outliner (str, optional): A block outlining style.
kwargs: Additional arguments to pass to the render() method.
Returns:
str: Markdown content.
"""
markdown = self.render()
markdown = self.render(**kwargs)
outline_approach = {
"newlines": outline_newlines,
"paragraphs": outline_paragraphs,
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "markline"
version = "0.3.1"
version = "0.3.2"
description = "Convert Markup to Markdown with a transformation pipeline."
authors = ["Hugh Cameron <[email protected]>"]
license = "MIT"
Expand Down
9 changes: 8 additions & 1 deletion tests/test_markline.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
local_html = ml.Markup(filepath=LOCAL_HTML)

with open("tests/test.html") as f:
soup_html = BeautifulSoup(f.read(), "lxml")
local_text = f.read()
soup_html = BeautifulSoup(local_text, "lxml")

with open("tests/test.md") as f:
local_md = f.read()
Expand Down Expand Up @@ -147,13 +148,19 @@ def test_new_token():


def test_fetch_url_content():
print(remote_html.to_html())
print(soup_html.prettify())
assert remote_html.to_html() == soup_html.prettify()


def test_fetch_local_content():
assert local_html.to_html() == soup_html.prettify()


def test_load_local_content():
assert local_html.to_html() == ml.Markup(text=local_text).to_html()


def test_gather_meta():
"""Test the gather_meta function."""
expected = {
Expand Down

0 comments on commit f2db997

Please sign in to comment.