Skip to content

Commit

Permalink
ok
Browse files Browse the repository at this point in the history
  • Loading branch information
tuna2134 committed May 24, 2023
1 parent 54bba78 commit 2dec575
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 23 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "mizu"
description = "Markdown library"
version = "0.2.0-alpha6"
version = "0.2.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
17 changes: 3 additions & 14 deletions mizu/__init__.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,8 @@
# Mizu's main module.

from .mizu import Mizu
from .parse import Mizu
from .mizu import Mizu as LowMizu
from .options import Options

from warnings import warn


def parse_ext(text: str, *args, **kwargs) -> str:
warn("parse_ext is deprecated, use Mizu class.", DeprecationWarning)
return Mizu(Options(*args, **kwargs)).parse(text)


def parse(*args, **kwargs) -> str:
warn("parse is deprecated, use Mizu class.", DeprecationWarning)
return Mizu(Options()).parse(*args, **kwargs)


__all__ = ("parse", "parse_ext", "Mizu", "Options")
__all__ = ("parse", "parse_ext", "Mizu", "Options", "LowMizu")
5 changes: 4 additions & 1 deletion mizu/mizu.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@ import asyncio

class Mizu:
def __init__(
self, options: Options = Options(), loop_: Optional[asyncio.AbstractEventLoop] = None
self, options: Options = Options()
) -> None:
...

def set_loop(self, loop: asyncio.AbstractEventLoop) -> None:
...

def parse(self, text: str) -> str:
...
Expand Down
39 changes: 39 additions & 0 deletions mizu/parse.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from .mizu import Mizu as _Mizu
from .options import Options
from asyncio import AbstractEventLoop
from typing import Optional


class Mizu:
"""
Markdown parser.
Args:
options (Options): Options for parser.
loop (AbstractEventLoop): Event loop
"""
def __init__(
self, options: Options = Options(),
loop: Optional[AbstractEventLoop] = None,
) -> None:
self.__parser = _Mizu(options)
if loop:
self.__parser.set_loop(loop)

def parse(self, text: str) -> str:
"""
Parse markdown text to html.
Args:
text (str): Markdown text.
"""
return self.__parser.parse(text)

async def aioparse(self, text: str) -> str:
"""
Parse markdown text to html (async version)
Args:
text (str): Markdown text
"""
return await self.__parser.aioparse(text)
17 changes: 11 additions & 6 deletions src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use pulldown_cmark::{html, Options, Parser};
///
/// Args:
/// options (Options): Options for parser.
/// loop (AbstractEventLoop): Event loop
#[pyclass]
#[pyo3(text_signature = "(options, loop/)")]
pub struct Mizu {
Expand All @@ -18,20 +17,26 @@ pub struct Mizu {
#[pymethods]
impl Mizu {
#[new]
#[args(options = "Options::empty()")]
#[pyo3(signature = (options = Options::empty()))]
pub fn new(
#[pyo3(from_py_with = "get_options")] options: Options,
loop_: Option<PyObject>,
) -> Self {
Mizu { options, loop_ }
Mizu {
options,
loop_: None,
}
}

fn set_loop(&mut self, loop_: PyObject) -> PyResult<()> {
self.loop_ = Some(loop_);
Ok(())
}

/// Parse markdown text to html.
///
/// Args:
/// text (str): Markdown text.
#[args(text)]
#[pyo3(text_signature = "(text, /)")]
#[pyo3(text_signature = "(text, /)", signature = (text))]
fn parse(&self, text: &str) -> PyResult<String> {
let parser: Parser = Parser::new_ext(text, self.options);

Expand Down
2 changes: 1 addition & 1 deletion tests/test_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@

@pytest.mark.asyncio
async def test_parse():
m = Mizu(loop_=asyncio.get_running_loop())
m = Mizu(loop=asyncio.get_running_loop())
assert await m.aioparse("# hello") == "<h1>hello</h1>\n"

0 comments on commit 2dec575

Please sign in to comment.