mwilliamson / mammoth.js

Convert Word documents (.docx files) to HTML
BSD 2-Clause "Simplified" License
4.88k stars 529 forks source link

Trying to use in standalone environment #28

Closed wirelessgizmo closed 10 years ago

wirelessgizmo commented 10 years ago

Heya,

Trying to use Mammoth in a Wordpress plugin by calling Mammoth from outside the context of the JS file you've provided in your Wordpress plugin.

Since I need to call the mammoth convert to HTML function in JS manually rather than attaching it to an event listener like you are I need the "mammoth" variable exposed outside the scope you have it in. I'm a bit stuch as to how to expose the scope, are you able to lend a hand at all or any advice?

mwilliamson commented 10 years ago

Mammoth uses browserify to create a standalone .js file. If you take a look at how the browser-demo directory, that gives an example of how to use Mammoth in the browser. Specifically:

Hope that makes sense!

wirelessgizmo commented 10 years ago

Heya thanks for the reply, I don't use or run node which I assume is what's required to run the make setup command, any chance you can shoot me through the JS file that would come as a result of this so I can include it in my library? Date: Thu, 11 Sep 2014 14:34:55 -0700 From: notifications@github.com To: mammoth.js@noreply.github.com CC: dan_churchill@hotmail.com Subject: Re: [mammoth.js] Trying to use in standalone environment (#28)

Mammoth uses browserify to create a standalone .js file. If you take a look at how the browser-demo directory, that gives an example of how to use Mammoth in the browser. Specifically:

Grab the project (cloning or downloading a tarball) Run make setup

Reference mammoth.browser.js or mammoth.browser.min.js in your page, which will make mammoth globally available (unless it detects a module system already loaded, in which case it will use that instead) Use mammoth.convertToHtml (or another function as appropriate) Hope that makes sense!

— Reply to this email directly or view it on GitHub. =

mwilliamson commented 10 years ago

I've uploaded the files to the releases page. Just a word of warning that I don't tend to put releases on there, so the latest uploaded version might not always be the latest version.

wirelessgizmo commented 9 years ago

Brilliant, that works perfectly. One thing I noticed is that italics and superscripts (what should become ) aren't being preserved. Is that not a feature of mammoth?

Date: Mon, 15 Sep 2014 12:23:19 -0700 From: notifications@github.com To: mammoth.js@noreply.github.com CC: dan_churchill@hotmail.com Subject: Re: [mammoth.js] Trying to use in standalone environment (#28)

I've uploaded the files to the releases page. Just a word of warning that I don't tend to put releases on there, so the latest uploaded version might not always be the latest version.

— Reply to this email directly or view it on GitHub. =

mwilliamson commented 9 years ago

Italics should work assuming they're applied using the italics button rather than as part of a style. There isn't any superscript support yet, but that shouldn't be too tricky to add (I might have time this weekend). If you could include an example document with bits that aren't converting, that'll make it easier for me to investigate. Also, any messages from the conversion process would be handy.

wirelessgizmo commented 9 years ago

Hey Michael, Thanks for the replies here, really appreciate it. Yep italics were applied in a style way so we've made sure the client uses the correct italic/bold mechanism. Find attached a sample word document with a snippet that includes superscripts that do get imported, but converted to plain text.

Date: Tue, 16 Sep 2014 04:58:42 -0700 From: notifications@github.com To: mammoth.js@noreply.github.com CC: dan_churchill@hotmail.com Subject: Re: [mammoth.js] Trying to use in standalone environment (#28)

Italics should work assuming they're applied using the italics button rather than as part of a style. There isn't any superscript support yet, but that shouldn't be too tricky to add (I might have time this weekend). If you could include an example document with bits that aren't converting, that'll make it easier for me to investigate. Also, any messages from the conversion process would be handy.

— Reply to this email directly or view it on GitHub. =

mwilliamson commented 9 years ago

Find attached a sample word document with a snippet that includes superscripts that do get imported, but converted to plain text.

Did you try attaching the file directly to the reply? I don't think GitHub Issues supports attachments, unfortunately. Could you put the file up somewhere on the Internet that I could download it, or alternatively just fire it off to my address (hello@zwobble.org)? Thanks!

Yep italics were applied in a style way so we've made sure the client uses the correct italic/bold mechanism.

Ah, in that case Mammoth intentionally ignores any formatting specified in the style. The easiest way of handling this case is probably to use a style mapping. For instance, if it's a run style called Important, you might end up with something like:

var options = {
    styleMap: [
        "r[style-name='Important'] => em",
    ]
};

Alternatively, if you have a paragraph style, it might make more sense to apply an appropriate CSS class:

var options = {
    styleMap: [
        "p[style-name='Important'] => p.important:fresh",
    ]
};

The README goes into more detail about style mappings, but hopefully that's enough to point you in the right direction.

wirelessgizmo commented 9 years ago

Hey, Yeah the superscripts currently get imported as their number values without any formatting. Is it possible to get Mammoth to import the superscripts and preserve the format in the HTML equivalent tags?

Date: Wed, 17 Sep 2014 12:20:06 -0700 From: notifications@github.com To: mammoth.js@noreply.github.com CC: dan_churchill@hotmail.com Subject: Re: [mammoth.js] Trying to use in standalone environment (#28)

Find attached a sample word document with a snippet that includes superscripts that do get imported, but converted to plain text.

Did you try attaching the file directly to the reply? I don't think GitHub Issues supports attachments, unfortunately. Could you put the file up somewhere on the Internet that I could download it, or alternatively just fire it off to my address (hello@zwobble.org)? Thanks!

Yep italics were applied in a style way so we've made sure the client uses the correct italic/bold mechanism.

Ah, in that case Mammoth intentionally ignores any formatting specified in the style. The easiest way of handling this case is probably to use a style mapping. For instance, if it's a run style called Important, you might end up with something like:

var options = { styleMap: [ "r[style-name='Important'] => em", ] };

Alternatively, if you have a paragraph style, it might make more sense to apply an appropriate CSS class:

var options = { styleMap: [ "p[style-name='Important'] => p.important:fresh", ] };

The README goes into more detail about style mappings, but hopefully that's enough to point you in the right direction.

— Reply to this email directly or view it on GitHub. =

mwilliamson commented 9 years ago

The latest version should support superscript and subscript text, let me know if not!