This version of the module is deprecated: Use moonbitlang/docx2html instead
Native MoonBit DOCX to HTML/Markdown converter ported from Mammoth
moon add bobzhang/docx2html///|
test "convert document model to html" {
let doc = @docx2html.document([
@docx2html.paragraph([@docx2html.text("Hello.")]),
])
let result = @docx2html.convert_document_to_html(doc)
inspect(result.value, content="<p>Hello.</p>")
}///|
test "build document model with helpers" {
let doc = @docx2html.document([
@docx2html.paragraph(
[
@docx2html.hyperlink(
[@docx2html.text("MoonBit")],
href=Some("https://www.moonbitlang.com"),
),
],
properties=@docx2html.paragraph_properties(style_name=Some("Heading 1")),
),
@docx2html.table([
@docx2html.table_row([
@docx2html.table_cell([@docx2html.paragraph([@docx2html.text("Cell")])]),
]),
]),
])
let result = @docx2html.convert_document_to_html(doc, style_map=[
"p[style-name='Heading 1'] => h2",
])
inspect(
result.value,
content="<h2><a href=\"https://www.moonbitlang.com\">MoonBit</a></h2><table><tr><td><p>Cell</p></td></tr></table>",
)
}///|
test "transform runs before conversion" {
let doc = @docx2html.document([
@docx2html.paragraph([@docx2html.run([@docx2html.text("Hello.")])]),
])
let transform_document = @docx2html.transform_runs(fn(_run) {
@docx2html.run([@docx2html.text("Goodbye.")])
})
let result = @docx2html.convert_document_to_html(doc, transform_document~)
inspect(result.value, content="<p>Goodbye.</p>")
let runs = @docx2html.document_descendants_where(doc, fn(element) {
element is Run(..)
})
inspect(runs.length(), content="1")
}moon run --target native cmd/main -- input.docx
moon run --target native cmd/main -- --output-format=markdown input.docx
moon run --target native cmd/main -- --style-map style-map input.docx output.html
moon run --target native cmd/main -- --output-dir out input.docx///|
test "custom image conversion" {
let image = @docx2html.Image::{
content_type: "image/png",
alt_text: Some("chart"),
data: b"abc",
}
let doc = @docx2html.document([@docx2html.paragraph([Image(image)])])
let convert_image = @docx2html.inline_image(fn(_image) {
{ "src": "/assets/chart.png" }
})
let result = @docx2html.convert_document_to_html(doc, convert_image~)
inspect(
result.value,
content="<p><img alt=\"chart\" src=\"/assets/chart.png\" /></p>",
)
}///|
test "combine conversion results" {
let combined = @docx2html.combine_results([
@docx2html.success("One"),
{ value: "Two", messages: [Warning("same")] },
{ value: "Three", messages: [Warning("same")] },
])
inspect(combined.value, content="OneTwoThree")
debug_inspect(combined.messages, content="[Warning(\"same\")]")
}pub(all) struct Comment {
comment_id : String
body : Array[DocumentElement]
author_name : String
author_initials : String
} derive(Eq, Debug)fn ConversionResult::flat_map(self : ConversionResult, f : (String) -> ConversionResult raise?) -> ConversionResult raise?fn ConversionResult::map(self : ConversionResult, f : (String) -> String raise?) -> ConversionResult raise?pub(all) enum DocumentElement {
Document(Array[DocumentElement], Array[Note], Array[Comment])
Paragraph(Array[DocumentElement], ParagraphProperties)
Run(Array[DocumentElement], RunProperties)
Text(String)
Tab
Checkbox(Bool)
Hyperlink(Array[DocumentElement], String?, String?, String?)
NoteReference(String, String)
CommentReference(String)
Image(Image)
Table(Array[DocumentElement], TableProperties)
TableRow(Array[DocumentElement], Bool)
TableCell(Array[DocumentElement], Int, Int)
Break(BreakType)
BookmarkStart(String)
} derive(Eq, Debug)pub(all) enum DocumentMatcher {
ParagraphMatcher(String?, StyleNameMatcher?, Numbering?)
RunMatcher(String?, StyleNameMatcher?)
RunPropertyMatcher(String)
HighlightMatcher(String?)
TableMatcher(String?, StyleNameMatcher?)
BreakMatcher(BreakType)
CommentReferenceMatcher
} derive(Eq, Debug)pub(all) struct Note {
note_type : String
note_id : String
body : Array[DocumentElement]
} derive(Eq, Debug)pub(all) struct RunProperties {
style_id : String?
style_name : String?
is_bold : Bool
is_underline : Bool
is_italic : Bool
is_strikethrough : Bool
is_all_caps : Bool
is_small_caps : Bool
vertical_alignment : VerticalAlignment
font : String?
font_size : Int?
highlight : String?
} derive(Eq, Debug)pub(all) struct StyleMapping {
from : DocumentMatcher
to : Array[HtmlPathElement]
} derive(Eq, Debug)fn comment(comment_id : String, body : Array[DocumentElement], author_name? : String, author_initials? : String) -> Commentfn convert_document_to_html(document : DocumentElement, style_map? : Array[String], include_default_style_map? : Bool, ignore_empty_paragraphs? : Bool, id_prefix? : String, pretty_print? : Bool, convert_image? : (Image) -> ImageConversion, transform_document? : (DocumentElement) -> DocumentElement) -> ConversionResultfn convert_document_to_markdown(document : DocumentElement, style_map? : Array[String], include_default_style_map? : Bool, ignore_empty_paragraphs? : Bool, id_prefix? : String, convert_image? : (Image) -> ImageConversion, transform_document? : (DocumentElement) -> DocumentElement) -> ConversionResultfn convert_to_html(docx : BytesView, style_map? : Array[String], include_default_style_map? : Bool, include_embedded_style_map? : Bool, ignore_empty_paragraphs? : Bool, id_prefix? : String, pretty_print? : Bool, convert_image? : (Image) -> ImageConversion, transform_document? : (DocumentElement) -> DocumentElement, external_file_access? : Bool, read_external_file? : (String) -> Bytes?) -> ConversionResult raise DocxErrorfn convert_to_markdown(docx : BytesView, style_map? : Array[String], include_default_style_map? : Bool, include_embedded_style_map? : Bool, ignore_empty_paragraphs? : Bool, id_prefix? : String, convert_image? : (Image) -> ImageConversion, transform_document? : (DocumentElement) -> DocumentElement, external_file_access? : Bool, read_external_file? : (String) -> Bytes?) -> ConversionResult raise DocxErrorfn document(children : Array[DocumentElement], notes? : Array[Note], comments? : Array[Comment]) -> DocumentElementfn document_descendants_where(element : DocumentElement, predicate : (DocumentElement) -> Bool) -> Array[DocumentElement]fn extract_raw_text(docx : BytesView, external_file_access? : Bool, read_external_file? : (String) -> Bytes?) -> ConversionResult raise DocxErrorfn hyperlink(children : Array[DocumentElement], href? : String?, anchor? : String?, target_frame? : String?) -> DocumentElementfn paragraph(children : Array[DocumentElement], properties? : ParagraphProperties) -> DocumentElementfn paragraph_properties(style_id? : String?, style_name? : String?, numbering? : Numbering?, alignment? : String?, indent? : Indent) -> ParagraphPropertiesfn read_docx(docx : BytesView, external_file_access? : Bool, read_external_file? : (String) -> Bytes?) -> DocumentElement raise DocxErrorfn read_xml_string(source : String, namespace_map? : Map[String, String]) -> XmlElement raise DocxErrorfn run_properties(style_id? : String?, style_name? : String?, is_bold? : Bool, is_underline? : Bool, is_italic? : Bool, is_strikethrough? : Bool, is_all_caps? : Bool, is_small_caps? : Bool, vertical_alignment? : VerticalAlignment, font? : String?, font_size? : Int?, highlight? : String?) -> RunPropertiesfn table_cell(children : Array[DocumentElement], col_span? : Int, row_span? : Int) -> DocumentElementfn transform_elements(element : DocumentElement, transform : (DocumentElement) -> DocumentElement) -> DocumentElementfn transform_paragraphs(transform : (DocumentElement) -> DocumentElement) -> ((DocumentElement) -> DocumentElement)fn transform_runs(transform : (DocumentElement) -> DocumentElement) -> ((DocumentElement) -> DocumentElement)fn xml_element(name : String, attributes? : Map[String, String], children? : Array[XmlNode]) -> XmlElementInstall
Download zipNative MoonBit DOCX to HTML/Markdown converter ported from Mammoth