Skip to content

Commit

Permalink
feat(parser): add color
Browse files Browse the repository at this point in the history
  • Loading branch information
kettei-sproutty committed Jan 17, 2024
1 parent b344754 commit 7dbbb82
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 6 deletions.
3 changes: 1 addition & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ wasm-bindgen = "0.2.84"
# all the `std::fmt` and `std::panicking` infrastructure, so isn't great for
# code size when deploying.
console_error_panic_hook = { version = "0.1.7", optional = true }
docx-rs = "0.4.7"
docx-rs = { git = "https://github.com/kettei-sproutty/docx-rs", branch = "main" }

[dev-dependencies]
wasm-bindgen-test = "0.3.34"
Expand Down
2 changes: 1 addition & 1 deletion example/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
<iframe
id="result"
style="display: none"
class="bg-gray-200 p-4 print:p-0 m-0 print:m-[25mm] text-black w-full h-full"
class="bg-gray-200 p-4 mx-auto max-w-[297mm] print:p-0 m-0 print:m-[25mm] text-black w-full h-full"
></iframe>
</div>
<script type="module" src="/src/main.ts"></script>
Expand Down
27 changes: 25 additions & 2 deletions src/parser/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,23 @@ use crate::element::{ElementChildren, ElementTag};
pub struct RunElement {
pub tags: Vec<ElementTag>,
pub text: String,
pub style: Vec<String>,
}

impl RunElement {
pub fn to_string(&self) -> String {
let mut string = String::new();

self.tags.iter().for_each(|tag| {
string.push_str(&format!("<{}>", tag.to_string()));
self.tags.iter().enumerate().for_each(|(index, tag)| {
if index == 0 {
string.push_str(&format!(
"<{} style=\"{}\">",
tag.to_string(),
self.style.join(";")
));
} else {
string.push_str(&format!("<{}>", tag.to_string()));
}
});

string.push_str(&self.text);
Expand All @@ -29,6 +38,7 @@ pub fn analyze_run_properties(run_properties: &RunProperty) -> RunElement {
let mut element = RunElement {
tags: vec![ElementTag::Span],
text: String::new(),
style: vec![],
};

if let Some(style) = &run_properties.style {
Expand All @@ -55,6 +65,19 @@ pub fn analyze_run_properties(run_properties: &RunProperty) -> RunElement {
element.tags.push(ElementTag::Mark);
};

if let Some(color) = &run_properties.color.as_ref() {
let value = &color.val;
if value.len().eq(&6) || value.len().eq(&8) {
if let Ok(_) = u32::from_str_radix(&value, 16) {
element.style.push(format!("color: #{}", value));
} else {
element.style.push(format!("color: {}", value));
}
} else {
element.style.push(format!("color: {}", value));
}
}

// TODO: superscript and subscript
// if run.run_property.vert_align.is_some() {
// if let Some(val) = &run.run_property.vert_align.as_ref().unwrap().val {
Expand Down

0 comments on commit 7dbbb82

Please sign in to comment.