From c3f394bb9b6a91ccde7b2255e46db463afc63652 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Thu, 7 May 2015 21:27:34 +0000 Subject: Use edge.js to run mammoth.js from .NET --- .gitignore | 9 + .nuget/NuGet.Config | 6 + .nuget/NuGet.targets | 144 + Mammoth.Tests/DocumentConverterTests.cs | 16 + Mammoth.Tests/Mammoth.Tests.csproj | 96 + Mammoth.Tests/Properties/AssemblyInfo.cs | 36 + Mammoth.Tests/TestDocuments/single-paragraph.docx | Bin 0 -> 12773 bytes Mammoth.Tests/packages.config | 5 + Mammoth.sln | 35 + Mammoth/DocumentConverter.cs | 34 + Mammoth/Mammoth.csproj | 94 + Mammoth/Message.cs | 19 + Mammoth/Properties/AssemblyInfo.cs | 36 + Mammoth/Result.cs | 19 + Mammoth/mammoth.browser.js | 26610 ++++++++++++++++++++ Mammoth/mammoth.edge.js | 18 + Mammoth/packages.config | 4 + 17 files changed, 27181 insertions(+) create mode 100644 .gitignore create mode 100644 .nuget/NuGet.Config create mode 100644 .nuget/NuGet.targets create mode 100644 Mammoth.Tests/DocumentConverterTests.cs create mode 100644 Mammoth.Tests/Mammoth.Tests.csproj create mode 100644 Mammoth.Tests/Properties/AssemblyInfo.cs create mode 100644 Mammoth.Tests/TestDocuments/single-paragraph.docx create mode 100644 Mammoth.Tests/packages.config create mode 100644 Mammoth.sln create mode 100644 Mammoth/DocumentConverter.cs create mode 100644 Mammoth/Mammoth.csproj create mode 100644 Mammoth/Message.cs create mode 100644 Mammoth/Properties/AssemblyInfo.cs create mode 100644 Mammoth/Result.cs create mode 100644 Mammoth/mammoth.browser.js create mode 100644 Mammoth/mammoth.edge.js create mode 100644 Mammoth/packages.config diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2512014 --- /dev/null +++ b/.gitignore @@ -0,0 +1,9 @@ +*.suo +*.user +/.nuget/*.exe +/packages +/Mammoth/bin +/Mammoth/obj +/Mammoth/edge +/Mammoth.Tests/bin +/Mammoth.Tests/obj diff --git a/.nuget/NuGet.Config b/.nuget/NuGet.Config new file mode 100644 index 0000000..67f8ea0 --- /dev/null +++ b/.nuget/NuGet.Config @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.nuget/NuGet.targets b/.nuget/NuGet.targets new file mode 100644 index 0000000..55c01f2 --- /dev/null +++ b/.nuget/NuGet.targets @@ -0,0 +1,144 @@ + + + + $(MSBuildProjectDirectory)\..\ + + + false + + + false + + + true + + + true + + + + + + + + + + + $([System.IO.Path]::Combine($(SolutionDir), ".nuget")) + + + + + $(SolutionDir).nuget + + + + $(MSBuildProjectDirectory)\packages.$(MSBuildProjectName.Replace(' ', '_')).config + $(MSBuildProjectDirectory)\packages.$(MSBuildProjectName).config + + + + $(MSBuildProjectDirectory)\packages.config + $(PackagesProjectConfig) + + + + + $(NuGetToolsPath)\NuGet.exe + @(PackageSource) + + "$(NuGetExePath)" + mono --runtime=v4.0.30319 "$(NuGetExePath)" + + $(TargetDir.Trim('\\')) + + -RequireConsent + -NonInteractive + + "$(SolutionDir) " + "$(SolutionDir)" + + + $(NuGetCommand) install "$(PackagesConfig)" -source "$(PackageSources)" $(NonInteractiveSwitch) $(RequireConsentSwitch) -solutionDir $(PaddedSolutionDir) + $(NuGetCommand) pack "$(ProjectPath)" -Properties "Configuration=$(Configuration);Platform=$(Platform)" $(NonInteractiveSwitch) -OutputDirectory "$(PackageOutputDir)" -symbols + + + + RestorePackages; + $(BuildDependsOn); + + + + + $(BuildDependsOn); + BuildPackage; + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Mammoth.Tests/DocumentConverterTests.cs b/Mammoth.Tests/DocumentConverterTests.cs new file mode 100644 index 0000000..9a977c5 --- /dev/null +++ b/Mammoth.Tests/DocumentConverterTests.cs @@ -0,0 +1,16 @@ +using NUnit.Framework; + +namespace Mammoth.Tests +{ + public class DocumentConverterTests + { + [Test] + public void SingleWordParagraphIsConvertedToSingleHtmlParagraph() + { + var documentConverter = new DocumentConverter(); + var result = documentConverter.ConvertToHtml("TestDocuments/single-paragraph.docx"); + Assert.AreEqual("

Walking on imported air

", result.Value); + Assert.IsEmpty(result.Messages); + } + } +} diff --git a/Mammoth.Tests/Mammoth.Tests.csproj b/Mammoth.Tests/Mammoth.Tests.csproj new file mode 100644 index 0000000..963e64c --- /dev/null +++ b/Mammoth.Tests/Mammoth.Tests.csproj @@ -0,0 +1,96 @@ + + + + + Debug + AnyCPU + {ED442448-256E-4574-BB1C-3303EF29B262} + Library + Properties + Mammoth.Tests + Mammoth.Tests + v4.0 + 512 + ..\ + true + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + ..\packages\NUnitTestAdapter.2.0.0\lib\nunit.core.dll + False + + + ..\packages\NUnitTestAdapter.2.0.0\lib\nunit.core.interfaces.dll + False + + + ..\packages\NUnit.2.6.4\lib\nunit.framework.dll + + + ..\packages\NUnitTestAdapter.2.0.0\lib\nunit.util.dll + False + + + ..\packages\NUnitTestAdapter.2.0.0\lib\NUnit.VisualStudio.TestAdapter.dll + False + + + + + + + + + + + + + + + + + PreserveNewest + + + + + {7398c88c-cb94-41c3-bef9-755bcf81f373} + Mammoth + + + + + + + + + + This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}. + + + + + \ No newline at end of file diff --git a/Mammoth.Tests/Properties/AssemblyInfo.cs b/Mammoth.Tests/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..c3c901e --- /dev/null +++ b/Mammoth.Tests/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("Mammoth.Tests")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("Microsoft")] +[assembly: AssemblyProduct("Mammoth.Tests")] +[assembly: AssemblyCopyright("Copyright © Microsoft 2015")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("ba4940f2-3760-4a66-b2a7-861164d661cc")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/Mammoth.Tests/TestDocuments/single-paragraph.docx b/Mammoth.Tests/TestDocuments/single-paragraph.docx new file mode 100644 index 0000000..09caf9a Binary files /dev/null and b/Mammoth.Tests/TestDocuments/single-paragraph.docx differ diff --git a/Mammoth.Tests/packages.config b/Mammoth.Tests/packages.config new file mode 100644 index 0000000..af221c7 --- /dev/null +++ b/Mammoth.Tests/packages.config @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file diff --git a/Mammoth.sln b/Mammoth.sln new file mode 100644 index 0000000..7749630 --- /dev/null +++ b/Mammoth.sln @@ -0,0 +1,35 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 2013 +VisualStudioVersion = 12.0.31101.0 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Mammoth", "Mammoth\Mammoth.csproj", "{7398C88C-CB94-41C3-BEF9-755BCF81F373}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".nuget", ".nuget", "{76999238-B232-4AB2-BFED-4C5427E7AD1C}" + ProjectSection(SolutionItems) = preProject + .nuget\NuGet.Config = .nuget\NuGet.Config + .nuget\NuGet.exe = .nuget\NuGet.exe + .nuget\NuGet.targets = .nuget\NuGet.targets + EndProjectSection +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Mammoth.Tests", "Mammoth.Tests\Mammoth.Tests.csproj", "{ED442448-256E-4574-BB1C-3303EF29B262}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {7398C88C-CB94-41C3-BEF9-755BCF81F373}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {7398C88C-CB94-41C3-BEF9-755BCF81F373}.Debug|Any CPU.Build.0 = Debug|Any CPU + {7398C88C-CB94-41C3-BEF9-755BCF81F373}.Release|Any CPU.ActiveCfg = Release|Any CPU + {7398C88C-CB94-41C3-BEF9-755BCF81F373}.Release|Any CPU.Build.0 = Release|Any CPU + {ED442448-256E-4574-BB1C-3303EF29B262}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {ED442448-256E-4574-BB1C-3303EF29B262}.Debug|Any CPU.Build.0 = Debug|Any CPU + {ED442448-256E-4574-BB1C-3303EF29B262}.Release|Any CPU.ActiveCfg = Release|Any CPU + {ED442448-256E-4574-BB1C-3303EF29B262}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/Mammoth/DocumentConverter.cs b/Mammoth/DocumentConverter.cs new file mode 100644 index 0000000..84c1f85 --- /dev/null +++ b/Mammoth/DocumentConverter.cs @@ -0,0 +1,34 @@ +using EdgeJs; +using System.Collections.Generic; +using System.IO; +using System.Reflection; +using System.Threading.Tasks; + +namespace Mammoth +{ + public class DocumentConverter + { + public Result ConvertToHtml(string path) + { + var mammothJs = ReadResource("Mammoth.mammoth.browser.js") + ReadResource("Mammoth.mammoth.edge.js"); + + var f = Edge.Func(mammothJs); + var result = f(File.ReadAllBytes(path)); + Task.WaitAll(result); + + return new Result((string)result.Result, new List()); + } + + private static string ReadResource(string resourceName) + { + var currentAssembly = Assembly.GetExecutingAssembly(); + using (var stream = currentAssembly.GetManifestResourceStream(resourceName)) + { + using (var reader = new StreamReader(stream)) + { + return reader.ReadToEnd(); + } + } + } + } +} diff --git a/Mammoth/Mammoth.csproj b/Mammoth/Mammoth.csproj new file mode 100644 index 0000000..82503d1 --- /dev/null +++ b/Mammoth/Mammoth.csproj @@ -0,0 +1,94 @@ + + + + + Debug + AnyCPU + {7398C88C-CB94-41C3-BEF9-755BCF81F373} + Library + Properties + Mammoth + Mammoth + v4.0 + 512 + ..\ + true + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + ..\packages\Edge.js.0.10.0\lib\EdgeJs.dll + + + + + + + + + + + + + + + + + + + + + PreserveNewest + + + PreserveNewest + + + PreserveNewest + + + PreserveNewest + + + + + + PreserveNewest + + + PreserveNewest + + + + + + + + This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}. + + + + + \ No newline at end of file diff --git a/Mammoth/Message.cs b/Mammoth/Message.cs new file mode 100644 index 0000000..6aa782b --- /dev/null +++ b/Mammoth/Message.cs @@ -0,0 +1,19 @@ +namespace Mammoth +{ + public class Message + { + public static Message Warning(string value) + { + return new Message(value); + } + + private readonly string _value; + + private Message(string value) + { + _value = value; + } + + public string Value { get { return _value; } } + } +} diff --git a/Mammoth/Properties/AssemblyInfo.cs b/Mammoth/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..3550d9c --- /dev/null +++ b/Mammoth/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("Mammoth")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("Microsoft")] +[assembly: AssemblyProduct("Mammoth")] +[assembly: AssemblyCopyright("Copyright © Microsoft 2015")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("0afd8667-fffe-4889-958e-fbb6023e04aa")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/Mammoth/Result.cs b/Mammoth/Result.cs new file mode 100644 index 0000000..7cb3277 --- /dev/null +++ b/Mammoth/Result.cs @@ -0,0 +1,19 @@ +using System.Collections.Generic; + +namespace Mammoth +{ + public class Result + { + private readonly T _value; + private readonly IList _messages; + + public Result(T value, IList messages) + { + _value = value; + _messages = messages; + } + + public T Value { get { return _value; } } + public IList Messages { get { return _messages; } } + } +} diff --git a/Mammoth/mammoth.browser.js b/Mammoth/mammoth.browser.js new file mode 100644 index 0000000..5ab3cd2 --- /dev/null +++ b/Mammoth/mammoth.browser.js @@ -0,0 +1,26610 @@ +// Module: mammoth@0.3.18 +// License: BSD +// +// Module: mammoth@0.3.18 => underscore@1.6.0 +// License: MIT +// +// Module: mammoth@0.3.18 => uglify-js@2.4.13 +// License: BSD 2-Clause +// +// Module: mammoth@0.3.18 => uglify-js@2.4.13 => uglify-to-browserify@1.0.2 +// License: MIT +// +// Module: mammoth@0.3.18 => uglify-js@2.4.13 => source-map@0.1.33 +// License: BSD +// +// Module: mammoth@0.3.18 => uglify-js@2.4.13 => source-map@0.1.33 => amdefine@0.1.0 +// License: BSD, MIT +// +// Module: mammoth@0.3.18 => uglify-js@2.4.13 => optimist@0.3.7 +// License: MIT/X11 +// +// Module: mammoth@0.3.18 => uglify-js@2.4.13 => optimist@0.3.7 => wordwrap@0.0.2 +// License: MIT/X11 +// +// Module: mammoth@0.3.18 => temp@0.7.0 +// License: MIT +// +// Module: mammoth@0.3.18 => temp@0.7.0 => rimraf@2.2.6 +// License: MIT +// +// Module: mammoth@0.3.18 => sax@0.5.8 +// License: BSD +// +// Module: mammoth@0.3.18 => nomnom@1.6.2 +// License: MIT +// +// Module: mammoth@0.3.18 => nomnom@1.6.2 => colors@0.5.1 +// License: Unknown license +// +// Module: mammoth@0.3.18 => mocha@2.1.0 +// License: MIT +// +// Module: mammoth@0.3.18 => mocha@2.1.0 => mkdirp@0.5.0 +// License: MIT +// +// Module: mammoth@0.3.18 => mocha@2.1.0 => mkdirp@0.5.0 => minimist@0.0.8 +// License: MIT +// +// Module: mammoth@0.3.18 => mocha@2.1.0 => jade@0.26.3 +// License: MIT +// +// Module: mammoth@0.3.18 => mocha@2.1.0 => jade@0.26.3 => mkdirp@0.3.0 +// License: MIT/X11 +// +// Module: mammoth@0.3.18 => mocha@2.1.0 => jade@0.26.3 => commander@0.6.1 +// License: MIT +// +// Module: mammoth@0.3.18 => mocha@2.1.0 => growl@1.8.1 +// License: MIT +// +// Module: mammoth@0.3.18 => mocha@2.1.0 => glob@3.2.3 +// License: BSD +// +// Module: mammoth@0.3.18 => mocha@2.1.0 => glob@3.2.3 => minimatch@0.2.14 +// License: MIT +// +// Module: mammoth@0.3.18 => mocha@2.1.0 => glob@3.2.3 => minimatch@0.2.14 => sigmund@1.0.0 +// License: BSD +// +// Module: mammoth@0.3.18 => mocha@2.1.0 => glob@3.2.3 => minimatch@0.2.14 => lru-cache@2.5.0 +// License: MIT +// +// Module: mammoth@0.3.18 => mocha@2.1.0 => glob@3.2.3 => inherits@2.0.1 +// License: ISC +// +// Module: mammoth@0.3.18 => mocha@2.1.0 => glob@3.2.3 => graceful-fs@2.0.3 +// License: BSD +// +// Module: mammoth@0.3.18 => mocha@2.1.0 => escape-string-regexp@1.0.2 +// License: MIT +// +// Module: mammoth@0.3.18 => mocha@2.1.0 => diff@1.0.8 +// License: BSD +// +// Module: mammoth@0.3.18 => mocha@2.1.0 => debug@2.0.0 +// License: MIT +// +// Module: mammoth@0.3.18 => mocha@2.1.0 => debug@2.0.0 => ms@0.6.2 +// License: Unknown license +// +// Module: mammoth@0.3.18 => mocha@2.1.0 => commander@2.3.0 +// License: MIT +// +// Module: mammoth@0.3.18 => lop@0.2.8 +// License: BSD +// +// Module: mammoth@0.3.18 => lop@0.2.8 => option@0.2.1 +// License: BSD +// +// Module: mammoth@0.3.18 => license-sniffer@0.1.2 +// License: BSD +// +// Module: mammoth@0.3.18 => license-sniffer@0.1.2 => underscore@1.4.4 +// License: MIT +// +// Module: mammoth@0.3.18 => license-sniffer@0.1.2 => q@0.9.7 +// License: MIT +// +// Module: mammoth@0.3.18 => license-sniffer@0.1.2 => optimist@0.3.7 +// License: MIT/X11 +// +// Module: mammoth@0.3.18 => license-sniffer@0.1.2 => optimist@0.3.7 => wordwrap@0.0.2 +// License: MIT/X11 +// +// Module: mammoth@0.3.18 => license-sniffer@0.1.2 => natural@0.1.29 +// License: MIT +// +// Module: mammoth@0.3.18 => license-sniffer@0.1.2 => natural@0.1.29 => sylvester@0.0.21 +// License: MIT +// +// Module: mammoth@0.3.18 => license-sniffer@0.1.2 => natural@0.1.29 => apparatus@0.0.8 +// License: MIT +// +// Module: mammoth@0.3.18 => license-sniffer@0.1.2 => markdown@0.4.0 +// License: MIT +// +// Module: mammoth@0.3.18 => license-sniffer@0.1.2 => markdown@0.4.0 => nopt@1.0.10 +// License: MIT +// +// Module: mammoth@0.3.18 => license-sniffer@0.1.2 => markdown@0.4.0 => nopt@1.0.10 => abbrev@1.0.5 +// License: MIT +// +// Module: mammoth@0.3.18 => jszip@2.4.0 +// License: MIT or GPLv3 +// +// Module: mammoth@0.3.18 => jszip@2.4.0 => pako@0.2.5 +// License: MIT +// +// Module: mammoth@0.3.18 => jshint@2.5.2 +// License: MIT +// +// Module: mammoth@0.3.18 => jshint@2.5.2 => strip-json-comments@0.1.3 +// License: MIT +// +// Module: mammoth@0.3.18 => jshint@2.5.2 => shelljs@0.3.0 +// License: BSD* +// +// Module: mammoth@0.3.18 => jshint@2.5.2 => minimatch@0.3.0 +// License: MIT +// +// Module: mammoth@0.3.18 => jshint@2.5.2 => minimatch@0.3.0 => sigmund@1.0.0 +// License: BSD +// +// Module: mammoth@0.3.18 => jshint@2.5.2 => minimatch@0.3.0 => lru-cache@2.5.0 +// License: MIT +// +// Module: mammoth@0.3.18 => jshint@2.5.2 => htmlparser2@3.7.2 +// License: MIT +// +// Module: mammoth@0.3.18 => jshint@2.5.2 => htmlparser2@3.7.2 => readable-stream@1.1.13-1 +// License: MIT +// +// Module: mammoth@0.3.18 => jshint@2.5.2 => htmlparser2@3.7.2 => readable-stream@1.1.13-1 => string_decoder@0.10.25-1 +// License: MIT +// +// Module: mammoth@0.3.18 => jshint@2.5.2 => htmlparser2@3.7.2 => readable-stream@1.1.13-1 => isarray@0.0.1 +// License: MIT +// +// Module: mammoth@0.3.18 => jshint@2.5.2 => htmlparser2@3.7.2 => readable-stream@1.1.13-1 => inherits@2.0.1 +// License: ISC +// +// Module: mammoth@0.3.18 => jshint@2.5.2 => htmlparser2@3.7.2 => readable-stream@1.1.13-1 => core-util-is@1.0.1 +// License: MIT +// +// Module: mammoth@0.3.18 => jshint@2.5.2 => htmlparser2@3.7.2 => entities@1.0.0 +// License: BSD-like +// +// Module: mammoth@0.3.18 => jshint@2.5.2 => htmlparser2@3.7.2 => domutils@1.5.0 +// License: BSD 2-Clause +// +// Module: mammoth@0.3.18 => jshint@2.5.2 => htmlparser2@3.7.2 => domhandler@2.2.0 +// License: BSD 2-Clause +// +// Module: mammoth@0.3.18 => jshint@2.5.2 => htmlparser2@3.7.2 => domelementtype@1.1.1 +// License: BSD 2-Clause +// +// Module: mammoth@0.3.18 => jshint@2.5.2 => exit@0.1.2 +// License: MIT +// +// Module: mammoth@0.3.18 => jshint@2.5.2 => console-browserify@1.1.0 +// License: MIT +// +// Module: mammoth@0.3.18 => jshint@2.5.2 => console-browserify@1.1.0 => date-now@0.1.4 +// License: MIT +// +// Module: mammoth@0.3.18 => jshint@2.5.2 => cli@0.6.3 +// License: MIT +// +// Module: mammoth@0.3.18 => jshint@2.5.2 => cli@0.6.3 => glob@3.2.11 +// License: BSD +// +// Module: mammoth@0.3.18 => jshint@2.5.2 => cli@0.6.3 => glob@3.2.11 => inherits@2.0.1 +// License: ISC +// +// Module: mammoth@0.3.18 => duck@0.1.11 +// License: BSD +// +// Module: mammoth@0.3.18 => browserify@3.33.0 +// License: MIT +// +// Module: mammoth@0.3.18 => browserify@3.33.0 => zlib-browserify@0.0.3 +// License: MIT +// +// Module: mammoth@0.3.18 => browserify@3.33.0 => zlib-browserify@0.0.3 => tape@0.2.2 +// License: MIT +// +// Module: mammoth@0.3.18 => browserify@3.33.0 => zlib-browserify@0.0.3 => tape@0.2.2 => jsonify@0.0.0 +// License: Public Domain +// +// Module: mammoth@0.3.18 => browserify@3.33.0 => zlib-browserify@0.0.3 => tape@0.2.2 => deep-equal@0.0.0 +// License: MIT/X11 +// +// Module: mammoth@0.3.18 => browserify@3.33.0 => vm-browserify@0.0.4 +// License: MIT +// +// Module: mammoth@0.3.18 => browserify@3.33.0 => vm-browserify@0.0.4 => indexof@0.0.1 +// License: Unknown license +// +// Module: mammoth@0.3.18 => browserify@3.33.0 => util@0.10.3 +// License: MIT +// +// Module: mammoth@0.3.18 => browserify@3.33.0 => url@0.7.9 +// License: Unknown license +// +// Module: mammoth@0.3.18 => browserify@3.33.0 => url@0.7.9 => querystring@0.1.0 +// License: Unknown license +// +// Module: mammoth@0.3.18 => browserify@3.33.0 => url@0.7.9 => punycode@1.0.0 +// License: MIT, GPL +// +// Module: mammoth@0.3.18 => browserify@3.33.0 => umd@2.0.0 +// License: MIT +// +// Module: mammoth@0.3.18 => browserify@3.33.0 => umd@2.0.0 => ruglify@1.0.0 +// License: MIT +// +// Module: mammoth@0.3.18 => browserify@3.33.0 => umd@2.0.0 => ruglify@1.0.0 => uglify-js@2.2.5 +// License: Unknown license +// +// Module: mammoth@0.3.18 => browserify@3.33.0 => umd@2.0.0 => ruglify@1.0.0 => uglify-js@2.2.5 => source-map@0.1.33 +// License: BSD +// +// Module: mammoth@0.3.18 => browserify@3.33.0 => umd@2.0.0 => ruglify@1.0.0 => uglify-js@2.2.5 => source-map@0.1.33 => amdefine@0.1.0 +// License: BSD, MIT +// +// Module: mammoth@0.3.18 => browserify@3.33.0 => umd@2.0.0 => ruglify@1.0.0 => uglify-js@2.2.5 => optimist@0.3.7 +// License: MIT/X11 +// +// Module: mammoth@0.3.18 => browserify@3.33.0 => umd@2.0.0 => ruglify@1.0.0 => uglify-js@2.2.5 => optimist@0.3.7 => wordwrap@0.0.2 +// License: MIT/X11 +// +// Module: mammoth@0.3.18 => browserify@3.33.0 => umd@2.0.0 => rfile@1.0.0 +// License: MIT +// +// Module: mammoth@0.3.18 => browserify@3.33.0 => umd@2.0.0 => rfile@1.0.0 => resolve@0.3.1 +// License: MIT +// +// Module: mammoth@0.3.18 => browserify@3.33.0 => umd@2.0.0 => rfile@1.0.0 => callsite@1.0.0 +// License: Unknown license +// +// Module: mammoth@0.3.18 => browserify@3.33.0 => tty-browserify@0.0.0 +// License: MIT +// +// Module: mammoth@0.3.18 => browserify@3.33.0 => timers-browserify@1.0.1 +// License: MIT +// +// Module: mammoth@0.3.18 => browserify@3.33.0 => timers-browserify@1.0.1 => process@0.5.2 +// License: MIT +// +// Module: mammoth@0.3.18 => browserify@3.33.0 => through@2.3.4 +// License: MIT +// +// Module: mammoth@0.3.18 => browserify@3.33.0 => syntax-error@1.1.0 +// License: MIT +// +// Module: mammoth@0.3.18 => browserify@3.33.0 => syntax-error@1.1.0 => esprima-fb@3001.1.0-dev-harmony-fb +// License: BSD +// +// Module: mammoth@0.3.18 => browserify@3.33.0 => subarg@0.0.1 +// License: MIT +// +// Module: mammoth@0.3.18 => browserify@3.33.0 => subarg@0.0.1 => minimist@0.0.8 +// License: MIT +// +// Module: mammoth@0.3.18 => browserify@3.33.0 => string_decoder@0.0.1 +// License: MIT +// +// Module: mammoth@0.3.18 => browserify@3.33.0 => stream-combiner@0.0.4 +// License: MIT +// +// Module: mammoth@0.3.18 => browserify@3.33.0 => stream-browserify@0.1.3 +// License: MIT +// +// Module: mammoth@0.3.18 => browserify@3.33.0 => stream-browserify@0.1.3 => process@0.5.2 +// License: MIT +// +// Module: mammoth@0.3.18 => browserify@3.33.0 => shell-quote@0.0.1 +// License: MIT +// +// Module: mammoth@0.3.18 => browserify@3.33.0 => shallow-copy@0.0.1 +// License: MIT +// +// Module: mammoth@0.3.18 => browserify@3.33.0 => resolve@0.6.2 +// License: MIT +// +// Module: mammoth@0.3.18 => browserify@3.33.0 => querystring-es3@0.2.0 +// License: MIT +// +// Module: mammoth@0.3.18 => browserify@3.33.0 => punycode@1.2.4 +// License: MIT, GPL +// +// Module: mammoth@0.3.18 => browserify@3.33.0 => path-browserify@0.0.0 +// License: MIT +// +// Module: mammoth@0.3.18 => browserify@3.33.0 => parents@0.0.2 +// License: MIT +// +// Module: mammoth@0.3.18 => browserify@3.33.0 => os-browserify@0.1.2 +// License: MIT +// +// Module: mammoth@0.3.18 => browserify@3.33.0 => module-deps@1.8.0 +// License: MIT +// +// Module: mammoth@0.3.18 => browserify@3.33.0 => module-deps@1.8.0 => minimist@0.0.8 +// License: MIT +// +// Module: mammoth@0.3.18 => browserify@3.33.0 => module-deps@1.8.0 => detective@3.1.0 +// License: MIT +// +// Module: mammoth@0.3.18 => browserify@3.33.0 => module-deps@1.8.0 => detective@3.1.0 => esprima-fb@3001.1.0-dev-harmony-fb +// License: BSD +// +// Module: mammoth@0.3.18 => browserify@3.33.0 => module-deps@1.8.0 => detective@3.1.0 => escodegen@1.1.0 +// License: BSD +// +// Module: mammoth@0.3.18 => browserify@3.33.0 => module-deps@1.8.0 => detective@3.1.0 => escodegen@1.1.0 => source-map@0.1.33 +// License: BSD +// +// Module: mammoth@0.3.18 => browserify@3.33.0 => module-deps@1.8.0 => detective@3.1.0 => escodegen@1.1.0 => source-map@0.1.33 => amdefine@0.1.0 +// License: BSD, MIT +// +// Module: mammoth@0.3.18 => browserify@3.33.0 => module-deps@1.8.0 => detective@3.1.0 => escodegen@1.1.0 => esutils@1.0.0 +// License: BSD +// +// Module: mammoth@0.3.18 => browserify@3.33.0 => module-deps@1.8.0 => detective@3.1.0 => escodegen@1.1.0 => estraverse@1.5.0 +// License: BSD +// +// Module: mammoth@0.3.18 => browserify@3.33.0 => module-deps@1.8.0 => detective@3.1.0 => escodegen@1.1.0 => esprima@1.0.4 +// License: BSD +// +// Module: mammoth@0.3.18 => browserify@3.33.0 => insert-module-globals@4.1.0 +// License: MIT +// +// Module: mammoth@0.3.18 => browserify@3.33.0 => insert-module-globals@4.1.0 => process@0.5.2 +// License: MIT +// +// Module: mammoth@0.3.18 => browserify@3.33.0 => insert-module-globals@4.1.0 => lexical-scope@1.1.0 +// License: MIT +// +// Module: mammoth@0.3.18 => browserify@3.33.0 => insert-module-globals@4.1.0 => lexical-scope@1.1.0 => astw@1.1.0 +// License: MIT +// +// Module: mammoth@0.3.18 => browserify@3.33.0 => insert-module-globals@4.1.0 => lexical-scope@1.1.0 => astw@1.1.0 => esprima-fb@3001.1.0-dev-harmony-fb +// License: BSD +// +// Module: mammoth@0.3.18 => browserify@3.33.0 => inherits@2.0.1 +// License: ISC +// +// Module: mammoth@0.3.18 => browserify@3.33.0 => https-browserify@0.0.0 +// License: MIT +// +// Module: mammoth@0.3.18 => browserify@3.33.0 => http-browserify@1.3.2 +// License: MIT/X11 +// +// Module: mammoth@0.3.18 => browserify@3.33.0 => http-browserify@1.3.2 => Base64@0.2.0 +// License: WTFPL +// +// Module: mammoth@0.3.18 => browserify@3.33.0 => glob@3.2.9 +// License: BSD +// +// Module: mammoth@0.3.18 => browserify@3.33.0 => glob@3.2.9 => minimatch@0.2.14 +// License: MIT +// +// Module: mammoth@0.3.18 => browserify@3.33.0 => glob@3.2.9 => minimatch@0.2.14 => sigmund@1.0.0 +// License: BSD +// +// Module: mammoth@0.3.18 => browserify@3.33.0 => glob@3.2.9 => minimatch@0.2.14 => lru-cache@2.5.0 +// License: MIT +// +// Module: mammoth@0.3.18 => browserify@3.33.0 => events@1.0.0 +// License: MIT +// +// Module: mammoth@0.3.18 => browserify@3.33.0 => duplexer@0.1.1 +// License: MIT +// +// Module: mammoth@0.3.18 => browserify@3.33.0 => domain-browser@1.1.1 +// License: MIT +// +// Module: mammoth@0.3.18 => browserify@3.33.0 => derequire@0.6.1 +// License: MIT +// +// Module: mammoth@0.3.18 => browserify@3.33.0 => derequire@0.6.1 => estraverse@1.5.0 +// License: BSD +// +// Module: mammoth@0.3.18 => browserify@3.33.0 => derequire@0.6.1 => esrefactor@0.1.0 +// License: BSD +// +// Module: mammoth@0.3.18 => browserify@3.33.0 => derequire@0.6.1 => esrefactor@0.1.0 => estraverse@0.0.4 +// License: BSD +// +// Module: mammoth@0.3.18 => browserify@3.33.0 => derequire@0.6.1 => esrefactor@0.1.0 => esprima@1.0.4 +// License: BSD +// +// Module: mammoth@0.3.18 => browserify@3.33.0 => derequire@0.6.1 => esrefactor@0.1.0 => escope@0.0.16 +// License: BSD +// +// Module: mammoth@0.3.18 => browserify@3.33.0 => derequire@0.6.1 => esprima-six@0.0.3 +// License: BSD +// +// Module: mammoth@0.3.18 => browserify@3.33.0 => deps-sort@0.1.1 +// License: MIT +// +// Module: mammoth@0.3.18 => browserify@3.33.0 => deps-sort@0.1.1 => minimist@0.0.8 +// License: MIT +// +// Module: mammoth@0.3.18 => browserify@3.33.0 => deps-sort@0.1.1 => JSONStream@0.6.4 +// License: Unknown license +// +// Module: mammoth@0.3.18 => browserify@3.33.0 => deps-sort@0.1.1 => JSONStream@0.6.4 => through@2.2.7 +// License: MIT +// +// Module: mammoth@0.3.18 => browserify@3.33.0 => deps-sort@0.1.1 => JSONStream@0.6.4 => jsonparse@0.0.5 +// License: MIT +// +// Module: mammoth@0.3.18 => browserify@3.33.0 => defined@0.0.0 +// License: MIT +// +// Module: mammoth@0.3.18 => browserify@3.33.0 => deep-equal@0.1.2 +// License: MIT +// +// Module: mammoth@0.3.18 => browserify@3.33.0 => crypto-browserify@1.0.9 +// License: MIT +// +// Module: mammoth@0.3.18 => browserify@3.33.0 => constants-browserify@0.0.1 +// License: MIT +// +// Module: mammoth@0.3.18 => browserify@3.33.0 => console-browserify@1.0.3 +// License: MIT +// +// Module: mammoth@0.3.18 => browserify@3.33.0 => concat-stream@1.4.4 +// License: MIT +// +// Module: mammoth@0.3.18 => browserify@3.33.0 => concat-stream@1.4.4 => typedarray@0.0.5 +// License: MIT +// +// Module: mammoth@0.3.18 => browserify@3.33.0 => concat-stream@1.4.4 => readable-stream@1.1.11 +// License: MIT +// +// Module: mammoth@0.3.18 => browserify@3.33.0 => concat-stream@1.4.4 => readable-stream@1.1.11 => string_decoder@0.10.25-1 +// License: MIT +// +// Module: mammoth@0.3.18 => browserify@3.33.0 => concat-stream@1.4.4 => readable-stream@1.1.11 => debuglog@0.0.2 +// License: MIT +// +// Module: mammoth@0.3.18 => browserify@3.33.0 => concat-stream@1.4.4 => readable-stream@1.1.11 => core-util-is@1.0.1 +// License: MIT +// +// Module: mammoth@0.3.18 => browserify@3.33.0 => commondir@0.0.1 +// License: MIT/X11 +// +// Module: mammoth@0.3.18 => browserify@3.33.0 => builtins@0.0.2 +// License: MIT +// +// Module: mammoth@0.3.18 => browserify@3.33.0 => buffer@2.1.5 +// License: MIT +// +// Module: mammoth@0.3.18 => browserify@3.33.0 => buffer@2.1.5 => ieee754@1.1.3 +// License: MIT +// +// Module: mammoth@0.3.18 => browserify@3.33.0 => buffer@2.1.5 => base64-js@0.0.6 +// License: MIT +// +// Module: mammoth@0.3.18 => browserify@3.33.0 => browser-resolve@1.2.2 +// License: MIT +// +// Module: mammoth@0.3.18 => browserify@3.33.0 => browser-resolve@1.2.2 => resolve@0.6.1 +// License: MIT +// +// Module: mammoth@0.3.18 => browserify@3.33.0 => browser-pack@2.0.1 +// License: MIT +// +// Module: mammoth@0.3.18 => browserify@3.33.0 => browser-pack@2.0.1 => combine-source-map@0.3.0 +// License: MIT +// +// Module: mammoth@0.3.18 => browserify@3.33.0 => browser-pack@2.0.1 => combine-source-map@0.3.0 => source-map@0.1.33 +// License: BSD +// +// Module: mammoth@0.3.18 => browserify@3.33.0 => browser-pack@2.0.1 => combine-source-map@0.3.0 => source-map@0.1.33 => amdefine@0.1.0 +// License: BSD, MIT +// +// Module: mammoth@0.3.18 => browserify@3.33.0 => browser-pack@2.0.1 => combine-source-map@0.3.0 => inline-source-map@0.3.0 +// License: MIT +// +// Module: mammoth@0.3.18 => browserify@3.33.0 => browser-pack@2.0.1 => combine-source-map@0.3.0 => convert-source-map@0.3.3 +// License: MIT +// +// Module: mammoth@0.3.18 => browserify@3.33.0 => browser-pack@2.0.1 => JSONStream@0.6.4 +// License: Unknown license +// +// Module: mammoth@0.3.18 => browserify@3.33.0 => browser-pack@2.0.1 => JSONStream@0.6.4 => through@2.2.7 +// License: MIT +// +// Module: mammoth@0.3.18 => browserify@3.33.0 => browser-pack@2.0.1 => JSONStream@0.6.4 => jsonparse@0.0.5 +// License: MIT +// +// Module: mammoth@0.3.18 => browserify@3.33.0 => assert@1.1.1 +// License: MIT +// +// Module: mammoth@0.3.18 => browserify@3.33.0 => assert@1.1.1 => util@0.10.2 +// License: Unknown license +// +// Module: mammoth@0.3.18 => browserify@3.33.0 => JSONStream@0.7.1 +// License: Unknown license +// +// Module: mammoth@0.3.18 => browserify@3.33.0 => JSONStream@0.7.1 => through@2.2.7 +// License: MIT +// +// Module: mammoth@0.3.18 => browserify@3.33.0 => JSONStream@0.7.1 => jsonparse@0.0.5 +// License: MIT +// +// Module: mammoth@0.3.18 => bluebird@2.3.11 +// License: MIT +// +// Module: mammoth@0.3.18 => async@0.2.10 +// License: MIT +// +!function(e){if("object"==typeof exports)module.exports=e();else if("function"==typeof define&&define.amd)define(e);else{var f;"undefined"!=typeof window?f=window:"undefined"!=typeof global?f=global:"undefined"!=typeof self&&(f=self),f.mammoth=e()}}(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);throw new Error("Cannot find module '"+o+"'")}var f=n[o]={exports:{}};t[o][0].call(f.exports,function(e){var n=t[o][1][e];return s(n?n:e)},f,f.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o 0) { + writeAll(); + writer.text(value); + } + } + + function asString() { + return writer.asString(); + } + + function append(otherHtmlGenerator) { + var otherString = otherHtmlGenerator.asString(); + if (otherString.length > 0) { + writeAll(); + } + writer.append(otherString); + } + + function satisfyPath(style) { + var firstFreshPartIndex = findIndex(style, function(part) { + return part.fresh; + }); + + var partIndex = 0; + while (partIndex < firstFreshPartIndex && stylePartMatchesStackElement(style[partIndex], stack[partIndex])) { + partIndex++; + } + while (stack.length > partIndex) { + close(); + } + for (; partIndex < style.length; partIndex++) { + open(style[partIndex]); + } + } + + function stylePartMatchesStackElement(stylePart, stackElement) { + return stackElement && stylePart.matchesElement(stackElement); + } + + function open(htmlPathElement, options) { + var stackElement = Object.create(htmlPathElement); + stackElement.written = false; + stack.push(stackElement); + if (options && options.alwaysWrite) { + writeAll(); + } + } + + function close() { + var element = stack.pop(); + if (element.written) { + writer.close(element.tagName); + } + } + + function closeAll() { + while (stack.length > 0) { + close(); + } + } + + function writeAll() { + stack.filter(function(element) { + return !element.written; + }).forEach(writeElement); + } + + function writeElement(element) { + element.written = true; + writer.open(element.tagName, element.attributes); + } + + function selfClosing(element) { + writer.selfClosing(element.tagName, element.attributes); + } + + return { + satisfyPath: satisfyPath, + text: text, + open: open, + close: close, + closeAll: closeAll, + asString: asString, + append: append, + selfClosing: selfClosing + }; +} + +function findIndex(array, predicate) { + for (var i = 0; i < array.length; i++) { + if (predicate(array[i])) { + return i; + } + } + return -1; +} + +},{"./writers":24}],14:[function(_dereq_,module,exports){ +var _ = _dereq_("underscore"); + +exports.topLevelElement = topLevelElement; +exports.elements = elements; +exports.element = element; + +function topLevelElement(tagName, attributes) { + return elements([element(tagName, attributes, {fresh: true})]); +} + +function elements(elementStyles) { + return elementStyles.map(function(elementStyle) { + if (_.isString(elementStyle)) { + return element(elementStyle); + } else { + return elementStyle; + } + }); +} + +function element(tagName, attributes, options) { + options = options || {}; + return new Element(tagName, attributes, options); +} + +function Element(tagName, attributes, options) { + var tagNames = {}; + if (_.isArray(tagName)) { + tagName.forEach(function(tagName) { + tagNames[tagName] = true; + }); + tagName = tagName[0]; + } else { + tagNames[tagName] = true; + } + + this.tagName = tagName; + this.tagNames = tagNames; + this.attributes = attributes || {}; + this.fresh = options.fresh; +} + +Element.prototype.matchesElement = function(element) { + return this.tagNames[element.tagName] && _.isEqual(this.attributes || {}, element.attributes || {}); +}; + +exports.empty = elements([]); + +},{"underscore":136}],15:[function(_dereq_,module,exports){ +var promises = _dereq_("./promises"); +var htmlPaths = _dereq_("./html-paths"); + +exports.inline = function(func) { + return function(element, html, messages, callback) { + return promises.when(func(element)).then(function(result) { + var attributes = {src: result.src}; + if (element.altText) { + attributes.alt = element.altText; + } + html.selfClosing(htmlPaths.element("img", attributes)); + }).then(callback, callback); + }; +}; + +},{"./html-paths":14,"./promises":18}],16:[function(_dereq_,module,exports){ +var docxReader = _dereq_("./docx/docx-reader"); +var DocumentConverter = _dereq_("./document-to-html").DocumentConverter; +var readStyle = _dereq_("./style-reader").readStyle; +var readOptions = _dereq_("./options-reader").readOptions; + +exports.convertToHtml = convertToHtml; +exports.convertToMarkdown = convertToMarkdown; +exports.convert = convert; +exports.extractRawText = extractRawText; +exports.images = _dereq_("./images"); +exports.transforms = _dereq_("./transforms"); +exports.underline = _dereq_("./underline"); + +function convertToHtml(input, options) { + return convert(input, options); +} + +function convertToMarkdown(input, options) { + var markdownOptions = Object.create(options || {}); + markdownOptions.outputFormat = "markdown"; + return convert(input, markdownOptions); +} + +function convert(input, options) { + var fullOptions = readOptions(options); + + return docxReader.read(input) + .then(function(documentResult) { + return documentResult.map(fullOptions.transformDocument); + }) + .then(function(documentResult) { + return convertDocumentToHtml(documentResult, fullOptions); + }); +} + +function convertDocumentToHtml(documentResult, options) { + var parsedOptions = Object.create(options); + parsedOptions.styleMap = (options.styleMap || []).map(readStyle); + var documentConverter = new DocumentConverter(parsedOptions); + return documentResult.flatMapThen(function(document) { + return documentConverter.convertToHtml(document); + }); +} + + +function extractRawText(input) { + return docxReader.read(input) + .then(function(documentResult) { + return documentResult.map(convertElementToRawText); + }); +} + +function convertElementToRawText(element) { + if (element.type === "text") { + return element.value; + } else { + var tail = element.type === "paragraph" ? "\n\n" : ""; + return (element.children || []).map(convertElementToRawText).join("") + tail; + } +} + +exports.styleMapping = function() { + throw new Error('Use a raw string instead of mammoth.styleMapping e.g. "p[style-name=\'Title\'] => h1" instead of mammoth.styleMapping("p[style-name=\'Title\'] => h1")'); +}; + +},{"./document-to-html":3,"./docx/docx-reader":7,"./images":15,"./options-reader":17,"./style-reader":20,"./transforms":21,"./underline":22}],17:[function(_dereq_,module,exports){ +exports.readOptions = readOptions; + + +var _ = _dereq_("underscore"); + + +var standardOptions = exports._standardOptions = { + styleMap: [ + "p[style-name='Normal'] => p:fresh", + + "p.Heading1 => h1:fresh", + "p.Heading2 => h2:fresh", + "p.Heading3 => h3:fresh", + "p.Heading4 => h4:fresh", + "p[style-name='Heading 1'] => h1:fresh", + "p[style-name='Heading 2'] => h2:fresh", + "p[style-name='Heading 3'] => h3:fresh", + "p[style-name='Heading 4'] => h4:fresh", + "p[style-name='heading 1'] => h1:fresh", + "p[style-name='heading 2'] => h2:fresh", + "p[style-name='heading 3'] => h3:fresh", + "p[style-name='heading 4'] => h4:fresh", + "p[style-name='heading 4'] => h4:fresh", + + "p[style-name='footnote text'] => p", + "r[style-name='footnote reference'] =>", + "p[style-name='endnote text'] => p", + "r[style-name='endnote reference'] =>", + + // LibreOffice + "p[style-name='Footnote'] => p", + "r[style-name='Footnote anchor'] =>", + "p[style-name='Endnote'] => p", + "r[style-name='Endnote anchor'] =>", + + "p:unordered-list(1) => ul > li:fresh", + "p:unordered-list(2) => ul|ol > li > ul > li:fresh", + "p:unordered-list(3) => ul|ol > li > ul|ol > li > ul > li:fresh", + "p:unordered-list(4) => ul|ol > li > ul|ol > li > ul|ol > li > ul > li:fresh", + "p:unordered-list(5) => ul|ol > li > ul|ol > li > ul|ol > li > ul|ol > li > ul > li:fresh", + "p:ordered-list(1) => ol > li:fresh", + "p:ordered-list(2) => ul|ol > li > ol > li:fresh", + "p:ordered-list(3) => ul|ol > li > ul|ol > li > ol > li:fresh", + "p:ordered-list(4) => ul|ol > li > ul|ol > li > ul|ol > li > ol > li:fresh", + "p:ordered-list(5) => ul|ol > li > ul|ol > li > ul|ol > li > ul|ol > li > ol > li:fresh" + ], + transformDocument: identity, + includeDefaultStyleMap: true +}; + +function readOptions(options) { + options = options || {}; + var fullOptions = {}; + _.extend(fullOptions, standardOptions, options); + + fullOptions.styleMap = readStyleMap(options.styleMap, fullOptions); + + return fullOptions; +} + +function readStyleMap(styleMap, options) { + var customStyleMap = readCustomStyleMap(styleMap); + return customStyleMap.concat(options.includeDefaultStyleMap ? standardOptions.styleMap : []); +} + +function readCustomStyleMap(styleMap) { + if (!styleMap) { + return []; + } else if (_.isString(styleMap)) { + return styleMap.split("\n").map(function(line) { + return line.trim(); + }); + } else { + return styleMap; + } +} + + +function identity(value) { + return value; +} + +},{"underscore":136}],18:[function(_dereq_,module,exports){ +var bluebird = _dereq_("bluebird/js/main/promise")(); + +exports.defer = bluebird.defer; +exports.when = bluebird.resolve; +exports.resolve = bluebird.resolve; +exports.all = bluebird.all; +exports.reject = bluebird.reject; +exports.promisify = bluebird.promisify; + +exports.nfcall = function(func) { + var args = Array.prototype.slice.call(arguments, 1); + var promisedFunc = bluebird.promisify(func); + return promisedFunc.apply(null, args); +}; + +bluebird.prototype.fail = bluebird.prototype.caught; + +},{"bluebird/js/main/promise":47}],19:[function(_dereq_,module,exports){ +var _ = _dereq_("underscore"); + + +exports.Result = Result; +exports.warning = warning; + + +function Result(value, messages) { + this.value = value; + this.messages = messages || []; +} + +Result.prototype.map = function(func) { + return new Result(func(this.value), this.messages); +}; + +Result.prototype.flatMap = function(func) { + var funcResult = func(this.value); + return new Result(funcResult.value, combineMessages([this, funcResult])); +}; + +Result.prototype.flatMapThen = function(func) { + var that = this; + return func(this.value).then(function(otherResult) { + return new Result(otherResult.value, combineMessages([that, otherResult])); + }); +}; + +Result.combine = function(results) { + var values = _.flatten(_.pluck(results, "value")); + var messages = combineMessages(results); + return new Result(values, messages); +}; + +function warning(message) { + return { + type: "warning", + message: message + }; +} + +function combineMessages(results) { + var messages = []; + _.flatten(_.pluck(results, "messages"), true).forEach(function(message) { + if (!containsMessage(messages, message)) { + messages.push(message); + } + }); + return messages; +} + +function containsMessage(messages, message) { + return _.find(messages, isSameMessage.bind(null, message)) !== undefined; +} + +function isSameMessage(first, second) { + return first.type === second.type && first.message === second.message; +} + +},{"underscore":136}],20:[function(_dereq_,module,exports){ +var _ = _dereq_("underscore"); +var lop = _dereq_("lop"); +var RegexTokeniser = lop.RegexTokeniser; + +var documentMatchers = _dereq_("./document-matchers"); +var htmlPaths = _dereq_("./html-paths"); + +exports.readHtmlPath = readHtmlPath; +exports.readDocumentMatcher = readDocumentMatcher; +exports.readStyle = readStyle; + + +function readStyle(string) { + return parseString(styleRule, string); +} + +function createStyleRule() { + return lop.rules.sequence( + lop.rules.sequence.capture(documentMatcherRule()), + lop.rules.tokenOfType("whitespace"), + lop.rules.tokenOfType("arrow"), + lop.rules.sequence.capture(lop.rules.optional(lop.rules.sequence( + lop.rules.tokenOfType("whitespace"), + lop.rules.sequence.capture(htmlPathRule()) + ).head())) + ).map(function(documentMatcher, htmlPath) { + return { + from: documentMatcher, + to: htmlPath.valueOrElse(htmlPaths.empty) + }; + }); +} + +function readDocumentMatcher(string) { + return parseString(documentMatcherRule(), string); +} + +function documentMatcherRule() { + var sequence = lop.rules.sequence; + + var identifierToConstant = function(identifier, constant) { + return lop.rules.then( + lop.rules.token("identifier", identifier), + function() { + return constant; + } + ); + }; + + var paragraphRule = identifierToConstant("p", documentMatchers.paragraph); + var runRule = identifierToConstant("r", documentMatchers.run); + + var elementTypeRule = lop.rules.firstOf("p or r", + paragraphRule, + runRule + ); + + var styleIdRule = lop.rules.then( + classRule, + function(styleId) { + return {styleId: styleId}; + } + ); + + var stringRule = lop.rules.then( + lop.rules.tokenOfType("string"), + function(value) { + return value.substring(1, value.length - 1); + } + ); + + var styleNameRule = lop.rules.then( + lop.rules.sequence( + lop.rules.tokenOfType("open-square-bracket"), + lop.rules.token("identifier", "style-name"), + lop.rules.tokenOfType("equals"), + lop.rules.sequence.capture(stringRule), + lop.rules.tokenOfType("close-square-bracket") + ).head(), + function(styleName) { + return {styleName: styleName}; + } + ); + + + var listTypeRule = lop.rules.firstOf("list type", + identifierToConstant("ordered-list", {isOrdered: true}), + identifierToConstant("unordered-list", {isOrdered: false}) + ); + var listRule = sequence( + lop.rules.tokenOfType("colon"), + sequence.capture(listTypeRule), + sequence.cut(), + lop.rules.tokenOfType("open-paren"), + sequence.capture(integerRule), + lop.rules.tokenOfType("close-paren") + ).map(function(listType, levelNumber) { + return { + list: { + isOrdered: listType.isOrdered, + levelIndex: levelNumber - 1 + } + }; + }); + var matcherSuffix = lop.rules.firstOf("matcher suffix", + styleIdRule, + styleNameRule, + listRule + ); + var matcherSuffixes = lop.rules.zeroOrMore(matcherSuffix); + + return sequence( + sequence.capture(elementTypeRule), + sequence.capture(matcherSuffixes) + ).map(function(createMatcher, suffixes) { + var matcherOptions = {}; + suffixes.forEach(function(suffix) { + _.extend(matcherOptions, suffix); + }); + return createMatcher(matcherOptions); + }); +} + +function readHtmlPath(string) { + return parseString(htmlPathRule(), string); +} + +function htmlPathRule() { + var capture = lop.rules.sequence.capture; + var whitespaceRule = lop.rules.tokenOfType("whitespace"); + var freshRule = lop.rules.then( + lop.rules.optional(lop.rules.sequence( + lop.rules.tokenOfType("colon"), + lop.rules.token("identifier", "fresh") + )), + function(option) { + return option.map(function() { + return true; + }).valueOrElse(false); + } + ); + + var tagNamesRule = lop.rules.oneOrMoreWithSeparator( + identifierRule, + lop.rules.tokenOfType("choice") + ); + + var styleElementRule = lop.rules.sequence( + capture(tagNamesRule), + capture(lop.rules.zeroOrMore(classRule)), + capture(freshRule) + ).map(function(tagName, classNames, fresh) { + var attributes = {}; + var options = {}; + if (classNames.length > 0) { + attributes["class"] = classNames.join(" "); + } + if (fresh) { + options.fresh = true; + } + return htmlPaths.element(tagName, attributes, options); + }); + + return lop.rules.then( + lop.rules.zeroOrMoreWithSeparator( + styleElementRule, + lop.rules.sequence( + whitespaceRule, + lop.rules.tokenOfType("gt"), + whitespaceRule + ) + ), + htmlPaths.elements + ); +} + +var identifierRule = lop.rules.tokenOfType("identifier"); +var integerRule = lop.rules.tokenOfType("integer"); + +var classRule = lop.rules.sequence( + lop.rules.tokenOfType("dot"), + lop.rules.sequence.capture(identifierRule) +).head(); + +function parseString(rule, string) { + var tokens = tokenise(string); + var parser = lop.Parser(); + var parseResult = parser.parseTokens(rule, tokens); + if (!parseResult.isSuccess()) { + throw new Error("Failed to parse: " + describeFailure(parseResult)); + } + return parseResult.value(); +} + +function describeFailure(parseResult) { + return parseResult.errors().map(describeError).join("\n"); + + function describeError(error) { + return error.describe(); + } +} + +function tokenise(string) { + var tokeniser = new RegexTokeniser([ + {name: "identifier", regex: /([a-zA-Z][a-zA-Z0-9\-]*)/}, + {name: "dot", regex: /\./}, + {name: "colon", regex: /:/}, + {name: "gt", regex: />/}, + {name: "whitespace", regex: /\s+/}, + {name: "arrow", regex: /=>/}, + {name: "equals", regex: /=/}, + {name: "open-paren", regex: /\(/}, + {name: "close-paren", regex: /\)/}, + {name: "open-square-bracket", regex: /\[/}, + {name: "close-square-bracket", regex: /\]/}, + {name: "string", regex: /'[^']*'/}, + {name: "integer", regex: /([0-9]+)/}, + {name: "choice", regex: /\|/} + ]); + return tokeniser.tokenise(string); +} + + +var styleRule = createStyleRule(); + +},{"./document-matchers":2,"./html-paths":14,"lop":122,"underscore":136}],21:[function(_dereq_,module,exports){ +exports.paragraph = function(transform) { + function transformElement(element) { + if (element.children) { + for (var i = 0; i < element.children.length; i++) { + element.children[i] = transformElement(element.children[i]); + } + } + if (element.type === "paragraph") { + return transform(element); + } else { + return element; + } + } + return transformElement; +}; + +},{}],22:[function(_dereq_,module,exports){ +var htmlPaths = _dereq_("./html-paths"); + + +exports.element = element; + +function element(name) { + return function(html) { + html.open(htmlPaths.element(name)); + }; +} + +},{"./html-paths":14}],23:[function(_dereq_,module,exports){ +var util = _dereq_("util"); +var _ = _dereq_("underscore"); + +exports.writer = writer; + +function writer(options) { + options = options || {}; + if (options.prettyPrint) { + return prettyWriter(); + } else { + return simpleWriter(); + } +} + + +var indentedElements = { + div: true, + p: true, + ul: true +}; + + +function prettyWriter() { + var indentationLevel = 0; + var indentation = " "; + var stack = []; + var start = true; + var inText = false; + + var writer = simpleWriter(); + + function open(tagName, attributes) { + indent(); + stack.push(tagName); + writer.open(tagName, attributes); + if (indentedElements[tagName]) { + indentationLevel++; + } + start = false; + } + + function close(tagName) { + if (indentedElements[tagName]) { + indentationLevel--; + } + indent(); + stack.pop(); + writer.close(tagName); + } + + function text(value) { + startText(); + writer.text(value.replace("\n", "\n" + indentation)); + } + + function selfClosing(tagName, attributes) { + indent(); + writer.selfClosing(tagName, attributes); + } + + function append(html) { + startText(); + writer.append(html.replace("\n", "\n" + indentation)); + } + + function insideIndentedElement() { + return stack.length === 0 || indentedElements[stack[stack.length - 1]]; + } + + function startText() { + if (!inText) { + indent(); + inText = true; + } + } + + function indent() { + inText = false; + if (!start && insideIndentedElement()) { + writer.append("\n"); + for (var i = 0; i < indentationLevel; i++) { + writer.append(indentation); + } + } + } + + return { + asString: writer.asString, + open: open, + close: close, + text: text, + selfClosing: selfClosing, + append: append + }; +} + + +function simpleWriter() { + var fragments = []; + + function open(tagName, attributes) { + var attributeString = generateAttributeString(attributes); + fragments.push(util.format("<%s%s>", tagName, attributeString)); + } + + function close(tagName) { + fragments.push(util.format("", tagName)); + } + + function selfClosing(tagName, attributes) { + var attributeString = generateAttributeString(attributes); + fragments.push(util.format("<%s%s />", tagName, attributeString)); + } + + function generateAttributeString(attributes) { + return _.map(attributes, function(value, key) { + return util.format(' %s="%s"', key, escapeHtml(value)); + }).join(""); + } + + function text(value) { + fragments.push(escapeHtml(value)); + } + + function append(html) { + fragments.push(html); + } + + function asString() { + return fragments.join(""); + } + + return { + asString: asString, + open: open, + close: close, + text: text, + selfClosing: selfClosing, + append: append + }; +} + + +function escapeHtml(value) { + return value + .replace(/&/g, '&') + .replace(/"/g, '"') + .replace(//g, '>'); +} + +},{"underscore":136,"util":81}],24:[function(_dereq_,module,exports){ +var htmlWriter = _dereq_("./html-writer"); +var markdownWriter = _dereq_("./markdown-writer"); + +exports.writer = writer; + + +function writer(options) { + options = options || {}; + if (options.outputFormat === "markdown") { + return markdownWriter.writer(); + } else { + return htmlWriter.writer(options); + } +} + +},{"./html-writer":23,"./markdown-writer":25}],25:[function(_dereq_,module,exports){ +var _ = _dereq_("underscore"); + + +function symmetricMarkdownElement(end) { + return markdownElement(end, end); +} + +function markdownElement(start, end) { + return function() { + return {start: start, end: end}; + }; +} + +function markdownLink(attributes) { + var href = attributes.href || ""; + if (href) { + return { + start: "[", + end: "](" + href + ")", + anchorPosition: "before" + }; + } else { + return {}; + } +} + +function markdownImage(attributes) { + var src = attributes.src || ""; + var altText = attributes.alt || ""; + if (src || altText) { + return {start: "![" + altText + "](" + src + ")"}; + } else { + return {}; + } +} + +function markdownList(options) { + return function(attributes, list) { + return { + start: list ? "\n" : "", + end: list ? "" : "\n", + list: { + isOrdered: options.isOrdered, + indent: list ? list.indent + 1 : 0, + count: 0 + } + }; + }; +} + +function markdownListItem(attributes, list, listItem) { + list = list || {indent: 0, isOrdered: false, count: 0}; + list.count++; + listItem.hasClosed = false; + + var bullet = list.isOrdered ? list.count + "." : "-"; + var start = repeatString("\t", list.indent) + bullet + " "; + + return { + start: start, + end: function() { + if (!listItem.hasClosed) { + listItem.hasClosed = true; + return "\n"; + } + } + }; +} + +var htmlToMarkdown = { + "p": markdownElement("", "\n\n"), + "br": markdownElement("", " \n"), + "ul": markdownList({isOrdered: false}), + "ol": markdownList({isOrdered: true}), + "li": markdownListItem, + "strong": symmetricMarkdownElement("__"), + "em": symmetricMarkdownElement("*"), + "a": markdownLink, + "img": markdownImage, +}; + +(function() { + for (var i = 1; i <= 6; i++) { + htmlToMarkdown["h" + i] = markdownElement(repeatString("#", i) + " ", "\n\n"); + } +})(); + +function repeatString(value, count) { + return new Array(count + 1).join(value); +} + +function markdownWriter() { + var fragments = []; + var elementStack = []; + var list = null; + var listItem = {}; + + function open(tagName, attributes) { + attributes = attributes || {}; + + var createElement = htmlToMarkdown[tagName] || function() { return {}; }; + var element = createElement(attributes, list, listItem); + elementStack.push({end: element.end, list: list}); + + if (element.list) { + list = element.list; + } + + var anchorBeforeStart = element.anchorPosition === "before"; + if (anchorBeforeStart) { + writeAnchor(attributes); + } + + fragments.push(element.start || ""); + if (!anchorBeforeStart) { + writeAnchor(attributes); + } + } + + function writeAnchor(attributes) { + if (attributes.id) { + fragments.push(''); + } + } + + function close(tagName) { + var element = elementStack.pop(); + list = element.list; + var end = _.isFunction(element.end) ? element.end() : element.end; + fragments.push(end || ""); + } + + function selfClosing(tagName, attributes) { + open(tagName, attributes); + close(tagName); + } + + function text(value) { + fragments.push(escapeMarkdown(value)); + } + + function append(html) { + fragments.push(html); + } + + function asString() { + return fragments.join(""); + } + + return { + asString: asString, + open: open, + close: close, + text: text, + selfClosing: selfClosing, + append: append + }; +} + +exports.writer = markdownWriter; + +function escapeMarkdown(value) { + return value + .replace(/\\/g, '\\\\') + .replace(/([\`\*_\{\}\[\]\(\)\#\+\-\.\!])/g, '\\$1'); +} + +},{"underscore":136}],26:[function(_dereq_,module,exports){ +var promises = _dereq_("./promises"); +var sax = _dereq_("sax"); +var _ = _dereq_("underscore"); + +exports.read = read; +exports.Element = Element; + +function read(xmlString, namespaceMap) { + namespaceMap = namespaceMap || {}; + + var finished = false; + var parser = sax.parser(true, {xmlns: true, position: false}); + + var rootElement = {children: []}; + var currentElement = rootElement; + var stack = []; + + var deferred = promises.defer(); + + parser.onopentag = function(node) { + var attributes = mapObject(node.attributes, function(attribute) { + return attribute.value; + }, mapName); + + var element = new Element(mapName(node), attributes); + currentElement.children.push(element); + stack.push(currentElement); + currentElement = element; + }; + + function mapName(node) { + if (node.uri) { + var mappedPrefix = namespaceMap[node.uri]; + var prefix; + if (mappedPrefix) { + prefix = mappedPrefix; + } else { + prefix = "{" + node.uri + "}"; + } + return prefix + ":" + node.local; + } else { + return node.local; + } + } + + parser.onclosetag = function(node) { + currentElement = stack.pop(); + }; + + parser.ontext = function(text) { + if (currentElement !== rootElement) { + currentElement.children.push({ + type: "text", + value: text + }); + } + }; + + parser.onend = function() { + if (!finished) { + finished = true; + deferred.resolve({ + root: rootElement.children[0] + }); + } + }; + + parser.onerror = function(error) { + if (!finished) { + finished = true; + deferred.reject(error); + } + }; + + parser.write(xmlString).close(); + + return deferred.promise; +} + +function mapObject(input, valueFunc, keyFunc) { + return _.reduce(input, function(result, value, key) { + var mappedKey = keyFunc.call(null, value, key, input); + result[mappedKey] = valueFunc.call(null, value, key, input); + return result; + }, {}); +} + +function Element(name, attributes, children) { + this.type = "element"; + this.name = name; + this.attributes = attributes || {}; + this.children = children || []; +} + +Element.prototype.first = function(name) { + return _.find(this.children, function(child) { + return child.name == name; + }); +}; + +Element.prototype.getElementsByTagName = function(name) { + var elements = _.filter(this.children, function(child) { + return child.name == name; + }); + return toElementList(elements); +}; + +Element.prototype.text = function() { + if (this.children.length === 0) { + return ""; + } else if (this.children.length !== 1 || this.children[0].type !== "text") { + throw new Error("Not implemented"); + } + return this.children[0].value; +}; + +var elementListPrototype = { + getElementsByTagName: function(name) { + return toElementList(_.flatten(this.map(function(element) { + return element.getElementsByTagName(name); + }, true))); + } +}; + +function toElementList(array) { + return _.extend(array, elementListPrototype); +} + +},{"./promises":18,"sax":135,"underscore":136}],27:[function(_dereq_,module,exports){ +(function (Buffer){ +var JSZip = _dereq_("jszip"); + +var promises = _dereq_("./promises"); + +exports.openArrayBuffer = openArrayBuffer; + +function openArrayBuffer(arrayBuffer) { + var zipFile = new JSZip(arrayBuffer); + function exists(name) { + return zipFile.file(name) !== null; + } + + function read(name, encoding) { + var array = zipFile.file(name).asUint8Array(); + var buffer = new Buffer(array); + if (encoding) { + return promises.when(buffer.toString(encoding)); + } else { + return promises.when(buffer); + } + } + return { + exists: exists, + read: read + }; +} + + +}).call(this,_dereq_("buffer").Buffer) +},{"./promises":18,"buffer":66,"jszip":91}],28:[function(_dereq_,module,exports){ +(function (process){ +/*global setImmediate: false, setTimeout: false, console: false */ +(function () { + + var async = {}; + + // global on the server, window in the browser + var root, previous_async; + + root = this; + if (root != null) { + previous_async = root.async; + } + + async.noConflict = function () { + root.async = previous_async; + return async; + }; + + function only_once(fn) { + var called = false; + return function() { + if (called) throw new Error("Callback was already called."); + called = true; + fn.apply(root, arguments); + } + } + + //// cross-browser compatiblity functions //// + + var _each = function (arr, iterator) { + if (arr.forEach) { + return arr.forEach(iterator); + } + for (var i = 0; i < arr.length; i += 1) { + iterator(arr[i], i, arr); + } + }; + + var _map = function (arr, iterator) { + if (arr.map) { + return arr.map(iterator); + } + var results = []; + _each(arr, function (x, i, a) { + results.push(iterator(x, i, a)); + }); + return results; + }; + + var _reduce = function (arr, iterator, memo) { + if (arr.reduce) { + return arr.reduce(iterator, memo); + } + _each(arr, function (x, i, a) { + memo = iterator(memo, x, i, a); + }); + return memo; + }; + + var _keys = function (obj) { + if (Object.keys) { + return Object.keys(obj); + } + var keys = []; + for (var k in obj) { + if (obj.hasOwnProperty(k)) { + keys.push(k); + } + } + return keys; + }; + + //// exported async module functions //// + + //// nextTick implementation with browser-compatible fallback //// + if (typeof process === 'undefined' || !(process.nextTick)) { + if (typeof setImmediate === 'function') { + async.nextTick = function (fn) { + // not a direct alias for IE10 compatibility + setImmediate(fn); + }; + async.setImmediate = async.nextTick; + } + else { + async.nextTick = function (fn) { + setTimeout(fn, 0); + }; + async.setImmediate = async.nextTick; + } + } + else { + async.nextTick = process.nextTick; + if (typeof setImmediate !== 'undefined') { + async.setImmediate = function (fn) { + // not a direct alias for IE10 compatibility + setImmediate(fn); + }; + } + else { + async.setImmediate = async.nextTick; + } + } + + async.each = function (arr, iterator, callback) { + callback = callback || function () {}; + if (!arr.length) { + return callback(); + } + var completed = 0; + _each(arr, function (x) { + iterator(x, only_once(function (err) { + if (err) { + callback(err); + callback = function () {}; + } + else { + completed += 1; + if (completed >= arr.length) { + callback(null); + } + } + })); + }); + }; + async.forEach = async.each; + + async.eachSeries = function (arr, iterator, callback) { + callback = callback || function () {}; + if (!arr.length) { + return callback(); + } + var completed = 0; + var iterate = function () { + iterator(arr[completed], function (err) { + if (err) { + callback(err); + callback = function () {}; + } + else { + completed += 1; + if (completed >= arr.length) { + callback(null); + } + else { + iterate(); + } + } + }); + }; + iterate(); + }; + async.forEachSeries = async.eachSeries; + + async.eachLimit = function (arr, limit, iterator, callback) { + var fn = _eachLimit(limit); + fn.apply(null, [arr, iterator, callback]); + }; + async.forEachLimit = async.eachLimit; + + var _eachLimit = function (limit) { + + return function (arr, iterator, callback) { + callback = callback || function () {}; + if (!arr.length || limit <= 0) { + return callback(); + } + var completed = 0; + var started = 0; + var running = 0; + + (function replenish () { + if (completed >= arr.length) { + return callback(); + } + + while (running < limit && started < arr.length) { + started += 1; + running += 1; + iterator(arr[started - 1], function (err) { + if (err) { + callback(err); + callback = function () {}; + } + else { + completed += 1; + running -= 1; + if (completed >= arr.length) { + callback(); + } + else { + replenish(); + } + } + }); + } + })(); + }; + }; + + + var doParallel = function (fn) { + return function () { + var args = Array.prototype.slice.call(arguments); + return fn.apply(null, [async.each].concat(args)); + }; + }; + var doParallelLimit = function(limit, fn) { + return function () { + var args = Array.prototype.slice.call(arguments); + return fn.apply(null, [_eachLimit(limit)].concat(args)); + }; + }; + var doSeries = function (fn) { + return function () { + var args = Array.prototype.slice.call(arguments); + return fn.apply(null, [async.eachSeries].concat(args)); + }; + }; + + + var _asyncMap = function (eachfn, arr, iterator, callback) { + var results = []; + arr = _map(arr, function (x, i) { + return {index: i, value: x}; + }); + eachfn(arr, function (x, callback) { + iterator(x.value, function (err, v) { + results[x.index] = v; + callback(err); + }); + }, function (err) { + callback(err, results); + }); + }; + async.map = doParallel(_asyncMap); + async.mapSeries = doSeries(_asyncMap); + async.mapLimit = function (arr, limit, iterator, callback) { + return _mapLimit(limit)(arr, iterator, callback); + }; + + var _mapLimit = function(limit) { + return doParallelLimit(limit, _asyncMap); + }; + + // reduce only has a series version, as doing reduce in parallel won't + // work in many situations. + async.reduce = function (arr, memo, iterator, callback) { + async.eachSeries(arr, function (x, callback) { + iterator(memo, x, function (err, v) { + memo = v; + callback(err); + }); + }, function (err) { + callback(err, memo); + }); + }; + // inject alias + async.inject = async.reduce; + // foldl alias + async.foldl = async.reduce; + + async.reduceRight = function (arr, memo, iterator, callback) { + var reversed = _map(arr, function (x) { + return x; + }).reverse(); + async.reduce(reversed, memo, iterator, callback); + }; + // foldr alias + async.foldr = async.reduceRight; + + var _filter = function (eachfn, arr, iterator, callback) { + var results = []; + arr = _map(arr, function (x, i) { + return {index: i, value: x}; + }); + eachfn(arr, function (x, callback) { + iterator(x.value, function (v) { + if (v) { + results.push(x); + } + callback(); + }); + }, function (err) { + callback(_map(results.sort(function (a, b) { + return a.index - b.index; + }), function (x) { + return x.value; + })); + }); + }; + async.filter = doParallel(_filter); + async.filterSeries = doSeries(_filter); + // select alias + async.select = async.filter; + async.selectSeries = async.filterSeries; + + var _reject = function (eachfn, arr, iterator, callback) { + var results = []; + arr = _map(arr, function (x, i) { + return {index: i, value: x}; + }); + eachfn(arr, function (x, callback) { + iterator(x.value, function (v) { + if (!v) { + results.push(x); + } + callback(); + }); + }, function (err) { + callback(_map(results.sort(function (a, b) { + return a.index - b.index; + }), function (x) { + return x.value; + })); + }); + }; + async.reject = doParallel(_reject); + async.rejectSeries = doSeries(_reject); + + var _detect = function (eachfn, arr, iterator, main_callback) { + eachfn(arr, function (x, callback) { + iterator(x, function (result) { + if (result) { + main_callback(x); + main_callback = function () {}; + } + else { + callback(); + } + }); + }, function (err) { + main_callback(); + }); + }; + async.detect = doParallel(_detect); + async.detectSeries = doSeries(_detect); + + async.some = function (arr, iterator, main_callback) { + async.each(arr, function (x, callback) { + iterator(x, function (v) { + if (v) { + main_callback(true); + main_callback = function () {}; + } + callback(); + }); + }, function (err) { + main_callback(false); + }); + }; + // any alias + async.any = async.some; + + async.every = function (arr, iterator, main_callback) { + async.each(arr, function (x, callback) { + iterator(x, function (v) { + if (!v) { + main_callback(false); + main_callback = function () {}; + } + callback(); + }); + }, function (err) { + main_callback(true); + }); + }; + // all alias + async.all = async.every; + + async.sortBy = function (arr, iterator, callback) { + async.map(arr, function (x, callback) { + iterator(x, function (err, criteria) { + if (err) { + callback(err); + } + else { + callback(null, {value: x, criteria: criteria}); + } + }); + }, function (err, results) { + if (err) { + return callback(err); + } + else { + var fn = function (left, right) { + var a = left.criteria, b = right.criteria; + return a < b ? -1 : a > b ? 1 : 0; + }; + callback(null, _map(results.sort(fn), function (x) { + return x.value; + })); + } + }); + }; + + async.auto = function (tasks, callback) { + callback = callback || function () {}; + var keys = _keys(tasks); + if (!keys.length) { + return callback(null); + } + + var results = {}; + + var listeners = []; + var addListener = function (fn) { + listeners.unshift(fn); + }; + var removeListener = function (fn) { + for (var i = 0; i < listeners.length; i += 1) { + if (listeners[i] === fn) { + listeners.splice(i, 1); + return; + } + } + }; + var taskComplete = function () { + _each(listeners.slice(0), function (fn) { + fn(); + }); + }; + + addListener(function () { + if (_keys(results).length === keys.length) { + callback(null, results); + callback = function () {}; + } + }); + + _each(keys, function (k) { + var task = (tasks[k] instanceof Function) ? [tasks[k]]: tasks[k]; + var taskCallback = function (err) { + var args = Array.prototype.slice.call(arguments, 1); + if (args.length <= 1) { + args = args[0]; + } + if (err) { + var safeResults = {}; + _each(_keys(results), function(rkey) { + safeResults[rkey] = results[rkey]; + }); + safeResults[k] = args; + callback(err, safeResults); + // stop subsequent errors hitting callback multiple times + callback = function () {}; + } + else { + results[k] = args; + async.setImmediate(taskComplete); + } + }; + var requires = task.slice(0, Math.abs(task.length - 1)) || []; + var ready = function () { + return _reduce(requires, function (a, x) { + return (a && results.hasOwnProperty(x)); + }, true) && !results.hasOwnProperty(k); + }; + if (ready()) { + task[task.length - 1](taskCallback, results); + } + else { + var listener = function () { + if (ready()) { + removeListener(listener); + task[task.length - 1](taskCallback, results); + } + }; + addListener(listener); + } + }); + }; + + async.waterfall = function (tasks, callback) { + callback = callback || function () {}; + if (tasks.constructor !== Array) { + var err = new Error('First argument to waterfall must be an array of functions'); + return callback(err); + } + if (!tasks.length) { + return callback(); + } + var wrapIterator = function (iterator) { + return function (err) { + if (err) { + callback.apply(null, arguments); + callback = function () {}; + } + else { + var args = Array.prototype.slice.call(arguments, 1); + var next = iterator.next(); + if (next) { + args.push(wrapIterator(next)); + } + else { + args.push(callback); + } + async.setImmediate(function () { + iterator.apply(null, args); + }); + } + }; + }; + wrapIterator(async.iterator(tasks))(); + }; + + var _parallel = function(eachfn, tasks, callback) { + callback = callback || function () {}; + if (tasks.constructor === Array) { + eachfn.map(tasks, function (fn, callback) { + if (fn) { + fn(function (err) { + var args = Array.prototype.slice.call(arguments, 1); + if (args.length <= 1) { + args = args[0]; + } + callback.call(null, err, args); + }); + } + }, callback); + } + else { + var results = {}; + eachfn.each(_keys(tasks), function (k, callback) { + tasks[k](function (err) { + var args = Array.prototype.slice.call(arguments, 1); + if (args.length <= 1) { + args = args[0]; + } + results[k] = args; + callback(err); + }); + }, function (err) { + callback(err, results); + }); + } + }; + + async.parallel = function (tasks, callback) { + _parallel({ map: async.map, each: async.each }, tasks, callback); + }; + + async.parallelLimit = function(tasks, limit, callback) { + _parallel({ map: _mapLimit(limit), each: _eachLimit(limit) }, tasks, callback); + }; + + async.series = function (tasks, callback) { + callback = callback || function () {}; + if (tasks.constructor === Array) { + async.mapSeries(tasks, function (fn, callback) { + if (fn) { + fn(function (err) { + var args = Array.prototype.slice.call(arguments, 1); + if (args.length <= 1) { + args = args[0]; + } + callback.call(null, err, args); + }); + } + }, callback); + } + else { + var results = {}; + async.eachSeries(_keys(tasks), function (k, callback) { + tasks[k](function (err) { + var args = Array.prototype.slice.call(arguments, 1); + if (args.length <= 1) { + args = args[0]; + } + results[k] = args; + callback(err); + }); + }, function (err) { + callback(err, results); + }); + } + }; + + async.iterator = function (tasks) { + var makeCallback = function (index) { + var fn = function () { + if (tasks.length) { + tasks[index].apply(null, arguments); + } + return fn.next(); + }; + fn.next = function () { + return (index < tasks.length - 1) ? makeCallback(index + 1): null; + }; + return fn; + }; + return makeCallback(0); + }; + + async.apply = function (fn) { + var args = Array.prototype.slice.call(arguments, 1); + return function () { + return fn.apply( + null, args.concat(Array.prototype.slice.call(arguments)) + ); + }; + }; + + var _concat = function (eachfn, arr, fn, callback) { + var r = []; + eachfn(arr, function (x, cb) { + fn(x, function (err, y) { + r = r.concat(y || []); + cb(err); + }); + }, function (err) { + callback(err, r); + }); + }; + async.concat = doParallel(_concat); + async.concatSeries = doSeries(_concat); + + async.whilst = function (test, iterator, callback) { + if (test()) { + iterator(function (err) { + if (err) { + return callback(err); + } + async.whilst(test, iterator, callback); + }); + } + else { + callback(); + } + }; + + async.doWhilst = function (iterator, test, callback) { + iterator(function (err) { + if (err) { + return callback(err); + } + if (test()) { + async.doWhilst(iterator, test, callback); + } + else { + callback(); + } + }); + }; + + async.until = function (test, iterator, callback) { + if (!test()) { + iterator(function (err) { + if (err) { + return callback(err); + } + async.until(test, iterator, callback); + }); + } + else { + callback(); + } + }; + + async.doUntil = function (iterator, test, callback) { + iterator(function (err) { + if (err) { + return callback(err); + } + if (!test()) { + async.doUntil(iterator, test, callback); + } + else { + callback(); + } + }); + }; + + async.queue = function (worker, concurrency) { + if (concurrency === undefined) { + concurrency = 1; + } + function _insert(q, data, pos, callback) { + if(data.constructor !== Array) { + data = [data]; + } + _each(data, function(task) { + var item = { + data: task, + callback: typeof callback === 'function' ? callback : null + }; + + if (pos) { + q.tasks.unshift(item); + } else { + q.tasks.push(item); + } + + if (q.saturated && q.tasks.length === concurrency) { + q.saturated(); + } + async.setImmediate(q.process); + }); + } + + var workers = 0; + var q = { + tasks: [], + concurrency: concurrency, + saturated: null, + empty: null, + drain: null, + push: function (data, callback) { + _insert(q, data, false, callback); + }, + unshift: function (data, callback) { + _insert(q, data, true, callback); + }, + process: function () { + if (workers < q.concurrency && q.tasks.length) { + var task = q.tasks.shift(); + if (q.empty && q.tasks.length === 0) { + q.empty(); + } + workers += 1; + var next = function () { + workers -= 1; + if (task.callback) { + task.callback.apply(task, arguments); + } + if (q.drain && q.tasks.length + workers === 0) { + q.drain(); + } + q.process(); + }; + var cb = only_once(next); + worker(task.data, cb); + } + }, + length: function () { + return q.tasks.length; + }, + running: function () { + return workers; + } + }; + return q; + }; + + async.cargo = function (worker, payload) { + var working = false, + tasks = []; + + var cargo = { + tasks: tasks, + payload: payload, + saturated: null, + empty: null, + drain: null, + push: function (data, callback) { + if(data.constructor !== Array) { + data = [data]; + } + _each(data, function(task) { + tasks.push({ + data: task, + callback: typeof callback === 'function' ? callback : null + }); + if (cargo.saturated && tasks.length === payload) { + cargo.saturated(); + } + }); + async.setImmediate(cargo.process); + }, + process: function process() { + if (working) return; + if (tasks.length === 0) { + if(cargo.drain) cargo.drain(); + return; + } + + var ts = typeof payload === 'number' + ? tasks.splice(0, payload) + : tasks.splice(0); + + var ds = _map(ts, function (task) { + return task.data; + }); + + if(cargo.empty) cargo.empty(); + working = true; + worker(ds, function () { + working = false; + + var args = arguments; + _each(ts, function (data) { + if (data.callback) { + data.callback.apply(null, args); + } + }); + + process(); + }); + }, + length: function () { + return tasks.length; + }, + running: function () { + return working; + } + }; + return cargo; + }; + + var _console_fn = function (name) { + return function (fn) { + var args = Array.prototype.slice.call(arguments, 1); + fn.apply(null, args.concat([function (err) { + var args = Array.prototype.slice.call(arguments, 1); + if (typeof console !== 'undefined') { + if (err) { + if (console.error) { + console.error(err); + } + } + else if (console[name]) { + _each(args, function (x) { + console[name](x); + }); + } + } + }])); + }; + }; + async.log = _console_fn('log'); + async.dir = _console_fn('dir'); + /*async.info = _console_fn('info'); + async.warn = _console_fn('warn'); + async.error = _console_fn('error');*/ + + async.memoize = function (fn, hasher) { + var memo = {}; + var queues = {}; + hasher = hasher || function (x) { + return x; + }; + var memoized = function () { + var args = Array.prototype.slice.call(arguments); + var callback = args.pop(); + var key = hasher.apply(null, args); + if (key in memo) { + callback.apply(null, memo[key]); + } + else if (key in queues) { + queues[key].push(callback); + } + else { + queues[key] = [callback]; + fn.apply(null, args.concat([function () { + memo[key] = arguments; + var q = queues[key]; + delete queues[key]; + for (var i = 0, l = q.length; i < l; i++) { + q[i].apply(null, arguments); + } + }])); + } + }; + memoized.memo = memo; + memoized.unmemoized = fn; + return memoized; + }; + + async.unmemoize = function (fn) { + return function () { + return (fn.unmemoized || fn).apply(null, arguments); + }; + }; + + async.times = function (count, iterator, callback) { + var counter = []; + for (var i = 0; i < count; i++) { + counter.push(i); + } + return async.map(counter, iterator, callback); + }; + + async.timesSeries = function (count, iterator, callback) { + var counter = []; + for (var i = 0; i < count; i++) { + counter.push(i); + } + return async.mapSeries(counter, iterator, callback); + }; + + async.compose = function (/* functions... */) { + var fns = Array.prototype.reverse.call(arguments); + return function () { + var that = this; + var args = Array.prototype.slice.call(arguments); + var callback = args.pop(); + async.reduce(fns, args, function (newargs, fn, cb) { + fn.apply(that, newargs.concat([function () { + var err = arguments[0]; + var nextargs = Array.prototype.slice.call(arguments, 1); + cb(err, nextargs); + }])) + }, + function (err, results) { + callback.apply(that, [err].concat(results)); + }); + }; + }; + + var _applyEach = function (eachfn, fns /*args...*/) { + var go = function () { + var that = this; + var args = Array.prototype.slice.call(arguments); + var callback = args.pop(); + return eachfn(fns, function (fn, cb) { + fn.apply(that, args.concat([cb])); + }, + callback); + }; + if (arguments.length > 2) { + var args = Array.prototype.slice.call(arguments, 2); + return go.apply(this, args); + } + else { + return go; + } + }; + async.applyEach = doParallel(_applyEach); + async.applyEachSeries = doSeries(_applyEach); + + async.forever = function (fn, callback) { + function next(err) { + if (err) { + if (callback) { + return callback(err); + } + throw err; + } + fn(next); + } + next(); + }; + + // AMD / RequireJS + if (typeof define !== 'undefined' && define.amd) { + define([], function () { + return async; + }); + } + // Node.js + else if (typeof module !== 'undefined' && module.exports) { + module.exports = async; + } + // included directly via