Skip to content

Commit

Permalink
Render and return HTML for passed Markdown
Browse files Browse the repository at this point in the history
  • Loading branch information
alerque committed May 15, 2020
1 parent e1f6e5d commit ae32d68
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,20 @@
extern crate mlua_derive;

use mlua::prelude::*;
use pulldown_cmark::{html, Options, Parser};

fn hello(_: &Lua, name: String) -> LuaResult<String> {
let a: String = String::from(format!("not you {} he he", name));
Ok(a)
fn render_html(_: &Lua, buffer: String) -> LuaResult<String> {
let options = Options::all();
let parser = Parser::new_ext(buffer.as_str(), options);
let mut html_output = String::new();
html::push_html(&mut html_output, parser);
Ok(html_output)
}

#[lua_module]
fn libvim_pandoc_syntax(lua: &Lua) -> LuaResult<LuaTable> {
let exports = lua.create_table()?;
exports.set("hello", lua.create_function(hello)?)?;
exports.set("render_html", lua.create_function(render_html)?)?;
Ok(exports)
}

Expand Down

1 comment on commit ae32d68

@fmoralesc
Copy link
Member

Choose a reason for hiding this comment

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

Basically the same as I did with the python interface

use pyo3::prelude::*;
use pyo3::wrap_pyfunction;
use pulldown_cmark::{Parser, Options, html};

#[pyclass]
struct OptionFlags {
    opts: Options
}

#[pymethods]
impl OptionFlags {
    #[new]
    fn new() -> Self {
        let opts = Options::all();
        OptionFlags { opts }
    }
}

#[pyfunction]
fn to_html(_py: Python, val: &str) -> PyResult<String> {
    let parser = Parser::new(val);

    let mut html_output = String::new();
    html::push_html(&mut html_output, parser);

    Ok(html_output)
}

#[pymodule]
fn libpulldowncmark(_py: Python, m: &PyModule) -> PyResult<()> {
    m.add_class::<OptionFlags>()?;

    m.add_wrapped(wrap_pyfunction!(to_html))?;

    Ok(())
}

Please sign in to comment.