Skip to content

Commit

Permalink
parse md from string rather than path
Browse files Browse the repository at this point in the history
  • Loading branch information
JR-1991 committed Jun 4, 2024
1 parent ca20913 commit cc09522
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 12 deletions.
3 changes: 2 additions & 1 deletion src/datamodel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,8 @@ impl DataModel {
/// # Returns
/// A data model
pub fn from_markdown(path: &Path) -> Result<Self, Box<dyn Error>> {
parse_markdown(path)
let content = fs::read_to_string(path)?;
parse_markdown(&content)
}

/// Parse a JSON schema and create a data model
Expand Down
5 changes: 4 additions & 1 deletion src/exporters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::datamodel::DataModel;
use clap::ValueEnum;
use lazy_static::lazy_static;
use minijinja::{context, Environment};
use pyo3::prelude::*;

lazy_static! {
/// Maps generic type names to Python-specific type names.
Expand Down Expand Up @@ -30,6 +31,7 @@ lazy_static! {
}

/// Enumeration of available templates.
#[pyclass]
#[derive(Debug, ValueEnum, Clone)]
pub enum Templates {
PythonDataclass,
Expand Down Expand Up @@ -192,7 +194,8 @@ mod tests {
/// A string containing the rendered template.
fn build_and_convert(template: Templates) -> String {
let path = Path::new("tests/data/model.md");
let mut model = parse_markdown(path).expect("Failed to parse markdown file");
let content = fs::read_to_string(path).expect("Could not read markdown file");
let mut model = parse_markdown(&content).expect("Failed to parse markdown file");
render_jinja_template(&template, &mut model)
.expect("Could not render template")
.trim()
Expand Down
12 changes: 2 additions & 10 deletions src/markdown/parser.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use std::collections::BTreeMap;
use std::error::Error;
use std::fs;
use std::path::Path;

use pulldown_cmark::{Event, Parser, Tag};
use regex::Regex;
Expand All @@ -28,16 +26,10 @@ enum ParserState {
/// # Returns
///
/// A `Result` containing a `DataModel` on success or an error on failure.
pub fn parse_markdown(path: &Path) -> Result<DataModel, Box<dyn Error>> {
if !path.exists() {
return Err("File does not exist".into());
}

let content = fs::read_to_string(path).expect("Could not read file");

pub fn parse_markdown(content: &str) -> Result<DataModel, Box<dyn Error>> {
// Remove all html tags
let re = Regex::new(r"<[^>]*>").unwrap();
let content = re.replace_all(&content, "").to_string();
let content = re.replace_all(content, "").to_string();

// Parse the frontmatter
let config = parse_frontmatter(content.as_str());
Expand Down

0 comments on commit cc09522

Please sign in to comment.