Skip to content

Commit

Permalink
Implement initial parsing for .umi => .html
Browse files Browse the repository at this point in the history
  • Loading branch information
SeppFS committed Jan 19, 2024
1 parent 197a1c3 commit bee173f
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 18 deletions.
18 changes: 11 additions & 7 deletions cli/src/compiler.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Entry module for unimarkup-rs.

use std::{
ffi::OsStr,
fs,
path::{Path, PathBuf},
};
Expand All @@ -23,13 +24,16 @@ use crate::log_id::{GeneralError, GeneralInfo};
///
/// Returns a [`GeneralError`] if error occurs during compilation.
pub fn compile(config: Config) -> Result<(), GeneralError> {
let source = fs::read_to_string(&config.input).map_err(|error| {
pipe!(
GeneralError::FileRead,
format!("Could not read file: '{:?}'", &config.input),
add: AddonKind::Info(format!("Cause: {}", error))
)
})?;
let source: String = match config.input.extension().and_then(OsStr::to_str) {
Some("umi") => unsafe { String::from_utf8_unchecked(fs::read(&config.input).unwrap()) },
_ => fs::read_to_string(&config.input).map_err(|error| {
pipe!(
GeneralError::FileRead,
format!("Could not read file: '{:?}'", &config.input),
add: AddonKind::Info(format!("Cause: {}", error))
)
})?,
};

let out_path = {
if let Some(ref out_file) = config.output.file {
Expand Down
5 changes: 4 additions & 1 deletion cli/tests/umi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use unimarkup_core::{
commons::config::Config,
inline::element::{Inline, InlineElement},
parser::{document::Document, elements::blocks::Block},
render::umi::Umi,
Unimarkup,
};

Expand Down Expand Up @@ -141,7 +142,9 @@ fn umi_supported() {
let mut umi = um.render_umi().unwrap();
let workbook = umi.create_workbook();

let looped_doc = &workbook.create_um().map_err(|_| panic!()).unwrap();
let looped_doc = &Umi::create_um(workbook.to_string().as_str(), &mut workbook.config)
.map_err(|_| panic!())
.unwrap();
let input = um.get_document();

assert!(
Expand Down
11 changes: 9 additions & 2 deletions core/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::ffi::OsStr;

pub use unimarkup_commons as commons;
pub use unimarkup_inline as inline;
pub use unimarkup_parser as parser;
Expand Down Expand Up @@ -25,8 +27,13 @@ impl Unimarkup {
/// * `um_content` - String containing Unimarkup elements.
/// * `config` - Unimarkup configuration to be used on top of preambles.
pub fn parse(um_content: &str, mut config: Config) -> Self {
Unimarkup {
doc: parser::parse_unimarkup(um_content, &mut config),
match config.input.extension().and_then(OsStr::to_str) {
Some("umi") => Unimarkup {
doc: Umi::create_um(um_content, &mut config).unwrap(),
},
_ => Unimarkup {
doc: parser::parse_unimarkup(um_content, &mut config),
},
}
}

Expand Down
20 changes: 12 additions & 8 deletions render/src/umi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,16 +312,20 @@ impl Umi {
}
}

pub fn create_um(&mut self) -> Result<Document, ParserError> {
self.elements.clear();
debug_assert!(!self.ods.is_empty());
pub fn create_um(um_content: &str, config: &mut Config) -> Result<Document, ParserError> {
let mut umi = Umi::with_um(
vec![],
config.clone(),
config.preamble.i18n.lang.to_string().to_owned(),
);
umi.ods = um_content.into();

let wb: WorkBook = read_ods_buf(&self.ods).unwrap_or_default();
let wb: WorkBook = read_ods_buf(&umi.ods).unwrap_or_default();
let sheet = wb.sheet(0);
let rows = sheet.used_grid_size().0;

for row_index in 2..rows {
self.elements.push(UmiRow::new(
umi.elements.push(UmiRow::new(
sheet
.cell(row_index, 0)
.unwrap_or_default()
Expand Down Expand Up @@ -369,9 +373,9 @@ impl Umi {
let mut um: Vec<Block> = vec![];

let mut index = 0;
while index < self.elements.len() {
if self.elements[index].depth == 0 {
um.push(self.read_row(index)?);
while index < umi.elements.len() {
if umi.elements[index].depth == 0 {
um.push(umi.read_row(index)?);
}
index += 1;
}
Expand Down

0 comments on commit bee173f

Please sign in to comment.