Paul Schneider 9 years ago
commit 7dcfe1227c
18 changed files with 787 additions and 473 deletions

27
.gitignore vendored

@ -1,27 +0,0 @@
.gitignore
appsettings.*.json
.idea/
.vscode/
.vs/
YavscWeb.userprefs
bin/
obj/
bower_components/
node_modules/
docfx_project/
debugcmd
undefined/
debugcode/
kestrel*.pid
kestrel*.log
scaffold.cmds
DataProtection-Keys
RSA-Params.json
Components/
packages/
build/
*.user
*.bak
*~

@ -0,0 +1,92 @@
using System.IO;
using Microsoft.AspNet.Authorization;
using Microsoft.AspNet.Mvc;
using System.Threading.Tasks;
using System.Web.Routing;
using Microsoft.AspNet.Mvc.ViewComponents;
using Microsoft.AspNet.Razor;
namespace Yavsc.ApiControllers
{
using Models;
[Route("api/pdfestimate"), Authorize]
public class PdfEstimateController : Controller
{
ApplicationDbContext dbContext;
DefaultViewComponentHelper helper;
IViewComponentDescriptorCollectionProvider provider;
IViewComponentInvokerFactory factory;
RazorEngineHost host;
RazorTemplateEngine engine;
IViewComponentSelector selector;
public PdfEstimateController(
IViewComponentDescriptorCollectionProvider provider,
IViewComponentSelector selector,
IViewComponentInvokerFactory factory,
ApplicationDbContext context)
{
this.selector = selector;
this.provider = provider;
this.factory = factory;
helper = new DefaultViewComponentHelper(provider, selector, factory);
dbContext = context;
var language = new CSharpRazorCodeLanguage();
host = new RazorEngineHost(language)
{
DefaultBaseClass = "RazorPage",
DefaultClassName = "Estimate",
DefaultNamespace = "Yavsc",
};
// Everyone needs the System namespace, right?
host.NamespaceImports.Add("System");
engine = new RazorTemplateEngine(host);
/*
GeneratorResults razorResult =
engine.GenerateCode(
) */
}
[HttpGet("get/{id}", Name = "Get"), Authorize]
public IActionResult Get(long id)
{
var filename = $"estimate-{id}.pdf";
var cd = new System.Net.Mime.ContentDisposition
{
// for example foo.bak
FileName = filename,
// always prompt the user for downloading, set to true if you want
// the browser to try to show the file inline
Inline = false,
};
FileInfo fi = new FileInfo(Path.Combine(Startup.UserBillsDirName, filename));
if (!fi.Exists) return Ok(new { Error = "Not generated" });
return File(fi.OpenRead(), "application/x-pdf", filename); ;
}
[HttpGet("estimate-{id}.tex", Name = "GetTex"), Authorize]
public IActionResult GetTex(long id)
{
Response.ContentType = "text/x-tex";
return ViewComponent("Estimate",new object[] { id, false });
}
[HttpPost("gen/{id}")]
public async Task<IActionResult> GeneratePdf(long id)
{
return ViewComponent("Estimate",new object[] { id, true } );
}
}
}

@ -36,10 +36,10 @@ namespace Yavsc.Controllers
}
[Route("Book/{id?}"), HttpGet]
public ActionResult Book(string id)
{
if (id == null) {
if (id == null)
{
throw new NotImplementedException("No Activity code");
}
@ -56,10 +56,10 @@ namespace Yavsc.Controllers
}
[Route("Book/{id}"), HttpPost]
public ActionResult Book(BookQuery bookQuery)
{
if (ModelState.IsValid) {
if (ModelState.IsValid)
{
var pro = _context.Performers.Include(
pr => pr.Performer
).FirstOrDefault(
@ -72,7 +72,8 @@ namespace Yavsc.Controllers
{
_context.BookQueries.Add(bookQuery);
}
else {
else
{
_context.BookQueries.Update(bookQuery);
}
_context.SaveChanges();
@ -100,16 +101,19 @@ namespace Yavsc.Controllers
return View("Estimate.tex", estimate);
}
[Produces("application/x-pdf"), Authorize, Route("estimate-{id}.pdf")]
public ViewResult EstimatePdf(long id)
[Authorize,Route("Estimate-{id}.pdf")]
public IActionResult EstimatePdf(long id)
{
ViewBag.TempDir = Startup.SiteSetup.TempDir;
ViewBag.BillsDir = Startup.UserBillsDirName;
var estimate = _context.Estimates.Include(x => x.Query)
.Include(x => x.Query.Client)
.Include(x => x.Query.PerformerProfile)
.Include(x => x.Query.PerformerProfile.OrganizationAddress)
.Include(x => x.Query.PerformerProfile.Performer)
.Include(e => e.Bill).FirstOrDefault(x => x.Id == id);
if (estimate==null)
throw new Exception("No data");
return View("Estimate.pdf",estimate);
}
}

@ -68,7 +68,6 @@ namespace Yavsc.Helpers
/// <summary>
/// Transforms a string of Markdown into HTML.
/// </summary>
/// <param name="helper">HtmlHelper - Not used, but required to make this an extension method.</param>
/// <param name="text">The Markdown that should be transformed.</param>
/// <param name="urlBaseLocation">The url Base Location.</param>
/// <returns>The HTML representation of the supplied Markdown.</returns>

@ -1,7 +1,68 @@
using System;
using System.IO;
using System.Linq;
using Microsoft.AspNet.Http;
using Microsoft.AspNet.Mvc;
using Microsoft.AspNet.Mvc.ModelBinding;
using Microsoft.AspNet.Mvc.Rendering;
using Microsoft.AspNet.Mvc.ViewEngines;
using Microsoft.AspNet.Mvc.ViewFeatures;
namespace Yavsc.Helpers
{
public class TeXString {
public class Replacement {
string target;
string replacement;
public Replacement(string target, string replacement){
this.target=target;
this.replacement=replacement;
}
public string Execute(string source)
{
return source.Replace(target,replacement);
}
}
public readonly static Replacement[] SpecialCharsToCommands =
{
new Replacement("<","\\textless"),
new Replacement(">","\\textgreater"),
new Replacement("©","\\copyright"),
new Replacement("®","\\textregistered"),
new Replacement("\\","\\textbackslash"),
new Replacement("™","\\texttrademark"),
new Replacement("¶","\\P"),
new Replacement("|","\\textbar"),
new Replacement("%","\\%"),
new Replacement("{","\\{"),
new Replacement("}","\\}"),
new Replacement("_","\\_"),
new Replacement("#","\\#"),
new Replacement("$","\\$"),
new Replacement("_","\\_"),
new Replacement("¿","\\textquestiondown"),
new Replacement("§","\\S"),
new Replacement("£","\\pounds"),
new Replacement("&","\\&"),
new Replacement("¡","\\textexclamdown"),
new Replacement("†","\\dag"),
new Replacement("","\\textendash")
};
string data;
public TeXString(string str) {
data = str;
foreach (var r in SpecialCharsToCommands) {
data = r.Execute(data);
}
}
override public string ToString()
{
return data;
}
}
public static class TeXHelpers
{
public static string NewLinesWith(this string target, string separator)
@ -11,5 +72,39 @@ namespace Yavsc.Helpers
return string.Join(separator, items);
}
public static TeXString ToTeX(string target, string lineSeparator="\n\\\\")
{
if (target==null) return null;
return new TeXString(target.NewLinesWith(lineSeparator));
}
public static string RenderViewToString(
this Controller controller, IViewEngine engine,
IHttpContextAccessor httpContextAccessor,
string viewName, object model)
{
using (var sw = new StringWriter())
{
if (engine == null)
throw new InvalidOperationException("no engine");
// try to find the specified view
controller.TryValidateModel(model);
ViewEngineResult viewResult = engine.FindPartialView(controller.ActionContext, viewName);
// create the associated context
ViewContext viewContext = new ViewContext();
viewContext.ActionDescriptor = controller.ActionContext.ActionDescriptor;
viewContext.HttpContext = controller.ActionContext.HttpContext;
viewContext.TempData = controller.TempData;
viewContext.View = viewResult.View;
viewContext.Writer = sw;
// write the render view with the given context to the stringwriter
viewResult.View.RenderAsync(viewContext);
viewResult.EnsureSuccessful();
return sw.GetStringBuilder().ToString();
}
}
}
}

@ -16,15 +16,32 @@ namespace Yavsc
/// </summary>
/// <returns></returns>
public string Authority { get; set; }
/// <summary>
/// Owner's email
/// </summary>
/// <returns></returns>
public EmailEntry Owner { get; set; }
/// <summary>
/// Administrator's email
/// </summary>
/// <returns></returns>
public EmailEntry Admin { get; set; }
/// <summary>
/// User's files directory
/// </summary>
/// <returns></returns>
public ThirdPartyFiles UserFiles { get; set; }
public string BusinessName { get; set; }
public string Street { get; set; }
public string PostalCode { get; set; }
public string CountryCode { get; set; }
/// <summary>
/// Specifies the directory where should be
/// generated pdf files using pandoc
/// </summary>
/// <returns>The temporary directory to use</returns>
public string TempDir { get; set; } = "Temp";
}
}

@ -12,6 +12,7 @@ namespace Yavsc
{
public static string UserFilesDirName { get; private set; }
public static FileServerOptions UserFilesOptions { get; private set; }
public void ConfigureFileServerApp(IApplicationBuilder app,
SiteSettings siteSettings, IHostingEnvironment env)
{

@ -1,6 +1,7 @@
using System;
using System.Globalization;
using System.IO;
using System.Reflection;
using System.Threading.Tasks;
using System.Web.Optimization;
@ -32,8 +33,11 @@ namespace Yavsc
public partial class Startup
{
public static string ConnectionString { get; private set; }
public static string UserBillsDirName { private set; get; }
public static string Authority { get; private set; }
public static string Audience { get; private set; }
public static SiteSettings SiteSetup { get; private set; }
private static ILogger logger;
public Startup(IHostingEnvironment env, IApplicationEnvironment appEnv)
{
@ -225,7 +229,18 @@ namespace Yavsc
RoleManager<IdentityRole> roleManager,
ILoggerFactory loggerFactory)
{
SiteSetup = siteSettings.Value;
Startup.UserFilesDirName = siteSettings.Value.UserFiles.DirName;
Startup.UserBillsDirName = siteSettings.Value.UserFiles.Bills;
// TODO implement an installation & upgrade procedure
// Create required directories
foreach (string dir in new string[] { Startup.UserFilesDirName, Startup.UserBillsDirName, SiteSetup.TempDir })
{
DirectoryInfo di = new DirectoryInfo(dir);
if (!di.Exists) di.Create();
}
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
logger = loggerFactory.CreateLogger<Startup>();

@ -0,0 +1,58 @@
using System;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNet.Mvc;
using Microsoft.Data.Entity;
using Yavsc.Models;
using Yavsc.Models.Billing;
using Yavsc.ViewModels.Gen;
namespace Yavsc.ViewComponents
{
public class EstimateViewComponent : ViewComponent
{
ApplicationDbContext dbContext;
public EstimateViewComponent(ApplicationDbContext dbContext)
{
this.dbContext = dbContext;
}
public async Task<IViewComponentResult> InvokeAsync(long id, bool toPdf = false)
{
Estimate estimate =
dbContext.Estimates.Include(x => x.Query)
.Include(x => x.Query.Client)
.Include(x => x.Query.PerformerProfile)
.Include(x => x.Query.PerformerProfile.OrganizationAddress)
.Include(x => x.Query.PerformerProfile.Performer)
.Include(e => e.Bill).FirstOrDefault(x => x.Id == id);
if (estimate == null)
throw new Exception("No data");
if (toPdf)
{
string tex = null;
var oldWriter = ViewComponentContext.ViewContext.Writer;
using (var writer = new StringWriter())
{
this.ViewComponentContext.ViewContext.Writer = writer;
var resultTex = View("Estimate_tex", estimate);
resultTex.Execute(this.ViewComponentContext);
tex = writer.ToString();
}
ViewComponentContext.ViewContext.Writer = oldWriter;
return this.View("Estimate_pdf",
new PdfGenerationViewModel{
TeXSource = tex,
DestDir = Startup.UserBillsDirName,
BaseFileName = $"estimate-{id}"
} );
}
return this.View("Estimate_tex", estimate);
}
}
}

@ -0,0 +1,14 @@
using System.ComponentModel.DataAnnotations;
namespace Yavsc.ViewModels.Gen
{
public class PdfGenerationViewModel
{
[Required]
public string TeXSource { get; set; }
[Required]
public string BaseFileName { get; set; }
[Required]
public string DestDir { get; set; }
}
}

@ -39,6 +39,12 @@
<a asp-action="Index">@SR["Back to List"]</a> |
@{ var filenametex = $"estimate-{Model.Id}.tex" ;
var filenamepdf = $"estimate-{Model.Id}.pdf" ;}
<a href="~/do/@filenametex" >Export au format LaTeX</a>
<a href="~/do/@filenamepdf" >Export au format LaTeX</a>
<a href="~/api/pdfestimate/estimate-@(Model.Id).tex" >Export au format LaTeX</a>
<form action="~/api/pdfestimate/gen/@Model.Id" method="POST">
<input type="submit" value="Générer le Pdf"/>
</form>
<a href="~/api/pdfestimate/get/@(Model.Id)" >Télécharger le document</a>
</p>

@ -1,55 +0,0 @@
@using System.Reflection
@using System.IO
@using Microsoft.Extensions.WebEncoders
@using System.Diagnostics
@using System.Text
@using Yavsc.Formatters
@model Estimate
@{
Layout = null;
ViewBag.Pdf = "";
ViewBag.TeX = "";
var writer = new System.IO.StringWriter();
var content = await Html.PartialAsync("Estimate.tex", Model );
content.WriteTo(writer, new TexEncoder());
var contentStr = writer.ToString();
string name = $"tmpestimtex-{Model.Id}";
string fullname = new FileInfo(
System.IO.Path.Combine(ViewBag.TempDir,name)).FullName;
FileInfo fi = new FileInfo(fullname + ".tex");
FileInfo fo = new FileInfo(fullname + ".pdf");
using (StreamWriter sw = new StreamWriter (fi.FullName))
{
sw.Write (contentStr);
}
if (!fi.Exists)
{
throw new Exception ("Source write failed");
}
using (Process p = new Process ()) {
p.StartInfo.WorkingDirectory = ViewBag.TempDir;
p.StartInfo = new ProcessStartInfo ();
p.StartInfo.UseShellExecute = false;
p.StartInfo.FileName = "/usr/bin/texi2pdf";
p.StartInfo.Arguments = $"--batch --build-dir=. -o {fo.FullName} {fi.FullName}";
p.Start ();
p.WaitForExit ();
if (p.ExitCode != 0) {
throw new Exception ("Pdf generation failed with exit code:" + p.ExitCode);
}
}
if (fo.Exists) {
UTF8Encoding utf8 = new UTF8Encoding();
using (StreamReader sr = new StreamReader (fo.FullName)) {
byte[] buffer = File.ReadAllBytes (fo.FullName);
ViewBag.Pdf = utf8.GetString(buffer,0,buffer.Length);
}
fo.Delete();
}
fi.Delete();
}
@ViewBag.Pdf

@ -0,0 +1,50 @@
@using System.Reflection
@using System.IO
@using Microsoft.Extensions.WebEncoders
@using System.Diagnostics
@using System.Text
@using Yavsc.Formatters
@model Yavsc.ViewModels.Gen.PdfGenerationViewModel
@{
string errorMsg = null;
var billdir = Model.DestDir;
var tempdir = Startup.SiteSetup.TempDir;
string name = Model.BaseFileName;
string fullname = new FileInfo(
System.IO.Path.Combine(tempdir,name)).FullName;
string ofullname = new FileInfo(
System.IO.Path.Combine(billdir,name)).FullName;
FileInfo fi = new FileInfo(fullname + ".tex");
FileInfo fo = new FileInfo(ofullname + ".pdf");
using (StreamWriter sw = new StreamWriter (fi.FullName))
{
sw.Write (Model.TeXSource);
}
if (!fi.Exists)
{
errorMsg = "Source write failed";
}
else {
using (Process p = new Process ()) {
p.StartInfo.WorkingDirectory = tempdir;
p.StartInfo = new ProcessStartInfo ();
p.StartInfo.UseShellExecute = false;
p.StartInfo.FileName = "/usr/bin/texi2pdf";
p.StartInfo.Arguments = $"--batch --build-dir=. -o {fo.FullName} {fi.FullName}";
p.Start ();
p.WaitForExit ();
if (p.ExitCode != 0) {
errorMsg = $"Pdf generation failed with exit code: {p.ExitCode}";
}
}
fi.Delete();
}
ViewBag.GenSuccess = fo.Exists;
}
@if (ViewBag.GenSuccess) {
@($"{name}.pdf")
} else {
@errorMsg
<text>Something went wrong ...</text>
}

@ -1,5 +1,6 @@
@model Estimate
@using Yavsc.Helpers
@using System.Globalization
@model Estimate
@{
Layout = null;
var pro = Model.Query.PerformerProfile;
@ -9,8 +10,8 @@
var proaddr = Model.Query?.PerformerProfile.OrganizationAddress.Address;
var proaddrn = (proaddr!=null) ? proaddr.NewLinesWith("\\\\\n") : null ;
var proaddrm = (proaddr!=null) ? proaddr.NewLinesWith(" - ") : null ;
}
\documentclass[french,11pt]{article}
}\documentclass[french,11pt]{article}
\usepackage{eurosym}
\usepackage{babel}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
@ -68,33 +69,29 @@
\def\FactureNum {@Model.Id.ToString()} % Numéro de facture
\def\FactureAcquittee {non} % Facture acquittée : oui/non
\def\FactureLieu {@proaddrn} % Lieu de l'édition de la facture
\def\FactureObjet {Facture : @Model.Title} % Objet du document
\def\FactureObjet {Facture : @TeXHelpers.ToTeX(Model.Title)} % Objet du document
% Description de la facture
\def\FactureDescr {
@Model.Description
@TeXHelpers.ToTeX(Model.Description)
}
% Infos Client
\def\ClientNom{@to.UserName} % Nom du client
\def\ClientNom{@TeXHelpers.ToTeX(to.UserName)} % Nom du client
\def\ClientAdresse{
% Adresse du client
@if (!string.IsNullOrWhiteSpace(PostalAddress)) {
<text> @PostalAddress\\</text> }
<text> @TeXHelpers.ToTeX(PostalAddress)\\</text> }
@if (!string.IsNullOrWhiteSpace(to.PhoneNumber)) {<text>
@to.PhoneNumber <text>\\</text>
@TeXHelpers.ToTeX(to.PhoneNumber)<text>\\</text>
</text>}
E-mail: @to.Email
E-mail: @TeXHelpers.ToTeX(to.Email)
}
% Liste des produits facturés : Désignation, prix
@if (Model.Bill!=null) { 
foreach (CommandLine line in Model.Bill) {
<text>
\AjouterService {@line.Description} {@line.Count} {@line.UnitaryCost}
</text>
} }
@if (Model.Bill!=null) { foreach (CommandLine line in Model.Bill) {
<text>\AjouterService{@TeXHelpers.ToTeX(line.Description)}{@line.Count}{@line.UnitaryCost.ToString("F2",CultureInfo.InvariantCulture)}
</text>} }
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
@ -109,24 +106,24 @@
\setlength{\parindent}{0pt}
\renewcommand{\headrulewidth}{0pt}
\cfoot{ @if (!string.IsNullOrWhiteSpace(from.UserName)) { @from.UserName }
@if (!string.IsNullOrWhiteSpace(proaddrm)) { <text> - @proaddrm </text> } \newline
\small{ E-mail: @from.Email
@if (!string.IsNullOrWhiteSpace(from.PhoneNumber)) { <text> - Téléphone fixe: @from.PhoneNumber </text> }
\cfoot{ @TeXHelpers.ToTeX(from.UserName) @if (!string.IsNullOrWhiteSpace(proaddrm)) { <text> - @TeXHelpers.ToTeX(proaddrm) </text> } \newline
\small{ E-mail: @TeXHelpers.ToTeX(from.Email) @if (!string.IsNullOrWhiteSpace(from.PhoneNumber)) { <text> - Téléphone fixe: @TeXHelpers.ToTeX(from.PhoneNumber) </text> }
}
}
\begin{document}
% Logo de la société
@if (from.Avatar != null) {<text>
\includegraphics{@from.Avatar}</text>
} else { <text>
%\includegraphics{logo.png}</text>
@if (from.Avatar != null) {
<text>\includegraphics{@from.Avatar}
</text>
} else {
<text>%\includegraphics{logo.png}
</text>
}
% Nom et adresse de la société
@from.UserName \\
@proaddrn
@TeXHelpers.ToTeX(from.UserName) \\
@TeXHelpers.ToTeX(proaddrn)
Facture n°\FactureNum
@ -151,7 +148,7 @@ Facture n°\FactureNum
\begin{center}
\begin{tabular}{lrrr}
\textbf{Désignation ~~~~~~} & \textbf{Prix unitaire} & \textbf{Quantité} & \textbf{Montant (EUR)} \\
\textbf{Désignation ~~~~~~} & \textbf{Prix unitaire} & \textbf{Quantité} & \textbf{Montant (\euro)} \\
\hline
\AfficheResultat{}
\end{tabular}

@ -0,0 +1,9 @@
\relax
\catcode `:\active
\catcode `;\active
\catcode `!\active
\catcode `?\active
\select@language{french}
\@writefile{toc}{\select@language{french}}
\@writefile{lof}{\select@language{french}}
\@writefile{lot}{\select@language{french}}

@ -140,5 +140,6 @@
"postrestore": "echo after restoring packages",
"prepublish": "gulp min",
"postpublish": "echo \" . ./contrib/postPublish.sh # to push in prod.\""
}
},
"embed": "Views/**/*.cshtml"
}

@ -0,0 +1,9 @@
\relax
\catcode `:\active
\catcode `;\active
\catcode `!\active
\catcode `?\active
\select@language{french}
\@writefile{toc}{\select@language{french}}
\@writefile{lof}{\select@language{french}}
\@writefile{lot}{\select@language{french}}

@ -2,101 +2,49 @@
/*
* to-markdown - an HTML to Markdown converter
*
* Copyright 2011-15, Dom Christie
* Copyright 2011+, Dom Christie
* Licenced under the MIT licence
*
*/
'use strict';
var toMarkdown;
var converters;
var mdConverters = require('./lib/md-converters');
var gfmConverters = require('./lib/gfm-converters');
var collapse = require('collapse-whitespace');
'use strict'
/*
* Set up window and document for Node.js
*/
var _window = (typeof window !== 'undefined' ? window : this), _document;
if (typeof document === 'undefined') {
_document = require('jsdom').jsdom();
}
else {
_document = document;
}
var toMarkdown
var converters
var mdConverters = require('./lib/md-converters')
var gfmConverters = require('./lib/gfm-converters')
var HtmlParser = require('./lib/html-parser')
var collapse = require('collapse-whitespace')
/*
* Utilities
*/
function trim(string) {
return string.replace(/^[ \r\n\t]+|[ \r\n\t]+$/g, '');
}
var blocks = ['address', 'article', 'aside', 'audio', 'blockquote', 'body',
'canvas', 'center', 'dd', 'dir', 'div', 'dl', 'dt', 'fieldset', 'figcaption',
'figure', 'footer', 'form', 'frameset', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6',
'header', 'hgroup', 'hr', 'html', 'isindex', 'li', 'main', 'menu', 'nav',
'noframes', 'noscript', 'ol', 'output', 'p', 'pre', 'section', 'table',
'tbody', 'td', 'tfoot', 'th', 'thead', 'tr', 'ul'
];
]
function isBlock (node) {
return blocks.indexOf(node.nodeName.toLowerCase()) !== -1;
return blocks.indexOf(node.nodeName.toLowerCase()) !== -1
}
var voids = [
'area', 'base', 'br', 'col', 'command', 'embed', 'hr', 'img', 'input',
'keygen', 'link', 'meta', 'param', 'source', 'track', 'wbr'
];
]
function isVoid (node) {
return voids.indexOf(node.nodeName.toLowerCase()) !== -1;
return voids.indexOf(node.nodeName.toLowerCase()) !== -1
}
/*
* Parsing HTML strings
*/
function canParseHtml() {
var Parser = _window.DOMParser, canParse = false;
// Adapted from https://gist.github.com/1129031
// Firefox/Opera/IE throw errors on unsupported types
try {
// WebKit returns null on unsupported types
if (new Parser().parseFromString('', 'text/html')) {
canParse = true;
}
} catch (e) {}
return canParse;
}
function createHtmlParser() {
var Parser = function () {};
Parser.prototype.parseFromString = function (string) {
var newDoc = _document.implementation.createHTMLDocument('');
if (string.toLowerCase().indexOf('<!doctype') > -1) {
newDoc.documentElement.innerHTML = string;
}
else {
newDoc.body.innerHTML = string;
}
return newDoc;
};
return Parser;
}
var HtmlParser = canParseHtml() ? _window.DOMParser : createHtmlParser();
function htmlToDom (string) {
var tree = new HtmlParser().parseFromString(string, 'text/html');
collapse(tree, isBlock);
return tree;
var tree = new HtmlParser().parseFromString(string, 'text/html')
collapse(tree.documentElement, isBlock)
return tree
}
/*
@ -104,20 +52,22 @@ function htmlToDom(string) {
*/
function bfsOrder (node) {
var inqueue = [node],
outqueue = [],
elem, children, i;
var inqueue = [node]
var outqueue = []
var elem
var children
var i
while (inqueue.length > 0) {
elem = inqueue.shift();
outqueue.push(elem);
children = elem.childNodes;
elem = inqueue.shift()
outqueue.push(elem)
children = elem.childNodes
for (i = 0; i < children.length; i++) {
if (children[i].nodeType === 1) { inqueue.push(children[i]); }
if (children[i].nodeType === 1) inqueue.push(children[i])
}
}
outqueue.shift();
return outqueue;
outqueue.shift()
return outqueue
}
/*
@ -125,17 +75,15 @@ function bfsOrder(node) {
*/
function getContent (node) {
var text = '';
var text = ''
for (var i = 0; i < node.childNodes.length; i++) {
if (node.childNodes[i].nodeType === 1) {
text += node.childNodes[i]._replacement;
}
else if (node.childNodes[i].nodeType === 3) {
text += node.childNodes[i].data;
text += node.childNodes[i]._replacement
} else if (node.childNodes[i].nodeType === 3) {
text += node.childNodes[i].data
} else continue
}
else { continue; }
}
return text;
return text
}
/*
@ -143,63 +91,62 @@ function getContent(node) {
*/
function outer (node, content) {
return node.cloneNode(false).outerHTML.replace('><', '>'+ content +'<');
return node.cloneNode(false).outerHTML.replace('><', '>' + content + '<')
}
function canConvert (node, filter) {
if (typeof filter === 'string') {
return filter === node.nodeName.toLowerCase();
return filter === node.nodeName.toLowerCase()
}
if (Array.isArray(filter)) {
return filter.indexOf(node.nodeName.toLowerCase()) !== -1;
}
else if (typeof filter === 'function') {
return filter.call(toMarkdown, node);
}
else {
throw new TypeError('`filter` needs to be a string, array, or function');
return filter.indexOf(node.nodeName.toLowerCase()) !== -1
} else if (typeof filter === 'function') {
return filter.call(toMarkdown, node)
} else {
throw new TypeError('`filter` needs to be a string, array, or function')
}
}
function isFlankedByWhitespace (side, node) {
var sibling, regExp, isFlanked;
var sibling
var regExp
var isFlanked
if (side === 'left') {
sibling = node.previousSibling;
regExp = / $/;
}
else {
sibling = node.nextSibling;
regExp = /^ /;
sibling = node.previousSibling
regExp = / $/
} else {
sibling = node.nextSibling
regExp = /^ /
}
if (sibling) {
if (sibling.nodeType === 3) {
isFlanked = regExp.test(sibling.nodeValue);
}
else if(sibling.nodeType === 1 && !isBlock(sibling)) {
isFlanked = regExp.test(sibling.textContent);
isFlanked = regExp.test(sibling.nodeValue)
} else if (sibling.nodeType === 1 && !isBlock(sibling)) {
isFlanked = regExp.test(sibling.textContent)
}
}
return isFlanked;
return isFlanked
}
function flankingWhitespace(node) {
var leading = '', trailing = '';
function flankingWhitespace (node, content) {
var leading = ''
var trailing = ''
if (!isBlock(node)) {
var hasLeading = /^[ \r\n\t]/.test(node.innerHTML),
hasTrailing = /[ \r\n\t]$/.test(node.innerHTML);
var hasLeading = /^[ \r\n\t]/.test(content)
var hasTrailing = /[ \r\n\t]$/.test(content)
if (hasLeading && !isFlankedByWhitespace('left', node)) {
leading = ' ';
leading = ' '
}
if (hasTrailing && !isFlankedByWhitespace('right', node)) {
trailing = ' ';
trailing = ' '
}
}
return { leading: leading, trailing: trailing };
return { leading: leading, trailing: trailing }
}
/*
@ -208,153 +155,157 @@ function flankingWhitespace(node) {
*/
function process (node) {
var replacement, content = getContent(node);
var replacement
var content = getContent(node)
// Remove blank nodes
if (!isVoid(node) && !/A/.test(node.nodeName) && /^\s*$/i.test(content)) {
node._replacement = '';
return;
if (!isVoid(node) && !/A|TH|TD/.test(node.nodeName) && /^\s*$/i.test(content)) {
node._replacement = ''
return
}
for (var i = 0; i < converters.length; i++) {
var converter = converters[i];
var converter = converters[i]
if (canConvert(node, converter.filter)) {
if (typeof converter.replacement !== 'function') {
throw new TypeError(
'`replacement` needs to be a function that returns a string'
);
)
}
var whitespace = flankingWhitespace(node);
var whitespace = flankingWhitespace(node, content)
if (whitespace.leading || whitespace.trailing) {
content = trim(content);
content = content.trim()
}
replacement = whitespace.leading +
converter.replacement.call(toMarkdown, content, node) +
whitespace.trailing;
break;
whitespace.trailing
break
}
}
node._replacement = replacement;
node._replacement = replacement
}
toMarkdown = function (input, options) {
options = options || {};
options = options || {}
if (typeof input !== 'string') {
throw new TypeError(input + ' is not a string');
throw new TypeError(input + ' is not a string')
}
if (input === '') {
return ''
}
// Escape potential ol triggers
input = input.replace(/(\d+)\. /g, '$1\\. ');
input = input.replace(/(\d+)\. /g, '$1\\. ')
var clone = htmlToDom(input).body,
nodes = bfsOrder(clone),
output;
var clone = htmlToDom(input).body
var nodes = bfsOrder(clone)
var output
converters = mdConverters.slice(0);
converters = mdConverters.slice(0)
if (options.gfm) {
converters = gfmConverters.concat(converters);
converters = gfmConverters.concat(converters)
}
if (options.converters) {
converters = options.converters.concat(converters);
converters = options.converters.concat(converters)
}
// Process through nodes in reverse (so deepest child elements are first).
for (var i = nodes.length - 1; i >= 0; i--) {
process(nodes[i]);
process(nodes[i])
}
output = getContent(clone);
output = getContent(clone)
return output.replace(/^[\t\r\n]+|[\t\r\n\s]+$/g, '')
.replace(/\n\s+\n/g, '\n\n')
.replace(/\n{3,}/g, '\n\n');
};
.replace(/\n{3,}/g, '\n\n')
}
toMarkdown.isBlock = isBlock;
toMarkdown.isVoid = isVoid;
toMarkdown.trim = trim;
toMarkdown.outer = outer;
toMarkdown.isBlock = isBlock
toMarkdown.isVoid = isVoid
toMarkdown.outer = outer
module.exports = toMarkdown;
module.exports = toMarkdown
},{"./lib/gfm-converters":2,"./lib/md-converters":3,"collapse-whitespace":4,"jsdom":7}],2:[function(require,module,exports){
'use strict';
},{"./lib/gfm-converters":2,"./lib/html-parser":3,"./lib/md-converters":4,"collapse-whitespace":7}],2:[function(require,module,exports){
'use strict'
function cell (content, node) {
var index = Array.prototype.indexOf.call(node.parentNode.childNodes, node);
var prefix = ' ';
if (index === 0) { prefix = '| '; }
return prefix + content + ' |';
var index = Array.prototype.indexOf.call(node.parentNode.childNodes, node)
var prefix = ' '
if (index === 0) prefix = '| '
return prefix + content + ' |'
}
var highlightRegEx = /highlight highlight-(\S+)/;
var highlightRegEx = /highlight highlight-(\S+)/
module.exports = [
{
filter: 'br',
replacement: function () {
return '\n';
return '\n'
}
},
{
filter: ['del', 's', 'strike'],
replacement: function (content) {
return '~~' + content + '~~';
return '~~' + content + '~~'
}
},
{
filter: function (node) {
return node.type === 'checkbox' && node.parentNode.nodeName === 'LI';
return node.type === 'checkbox' && node.parentNode.nodeName === 'LI'
},
replacement: function (content, node) {
return (node.checked ? '[x]' : '[ ]') + ' ';
return (node.checked ? '[x]' : '[ ]') + ' '
}
},
{
filter: ['th', 'td'],
replacement: function (content, node) {
return cell(content, node);
return cell(content, node)
}
},
{
filter: 'tr',
replacement: function (content, node) {
var borderCells = '';
var alignMap = { left: ':--', right: '--:', center: ':-:' };
var borderCells = ''
var alignMap = { left: ':--', right: '--:', center: ':-:' }
if (node.parentNode.nodeName === 'THEAD') {
for (var i = 0; i < node.childNodes.length; i++) {
var align = node.childNodes[i].attributes.align;
var border = '---';
var align = node.childNodes[i].attributes.align
var border = '---'
if (align) { border = alignMap[align.value] || border; }
if (align) border = alignMap[align.value] || border
borderCells += cell(border, node.childNodes[i]);
borderCells += cell(border, node.childNodes[i])
}
}
return '\n' + content + (borderCells ? '\n' + borderCells : '');
return '\n' + content + (borderCells ? '\n' + borderCells : '')
}
},
{
filter: 'table',
replacement: function (content) {
return '\n\n' + content + '\n\n';
return '\n\n' + content + '\n\n'
}
},
{
filter: ['thead', 'tbody', 'tfoot'],
replacement: function (content) {
return content;
return content
}
},
@ -363,10 +314,10 @@ module.exports = [
filter: function (node) {
return node.nodeName === 'PRE' &&
node.firstChild &&
node.firstChild.nodeName === 'CODE';
node.firstChild.nodeName === 'CODE'
},
replacement: function (content, node) {
return '\n\n```\n' + node.firstChild.textContent + '\n```\n\n';
return '\n\n```\n' + node.firstChild.textContent + '\n```\n\n'
}
},
@ -375,232 +326,356 @@ module.exports = [
filter: function (node) {
return node.nodeName === 'PRE' &&
node.parentNode.nodeName === 'DIV' &&
highlightRegEx.test(node.parentNode.className);
highlightRegEx.test(node.parentNode.className)
},
replacement: function (content, node) {
var language = node.parentNode.className.match(highlightRegEx)[1];
return '\n\n```' + language + '\n' + node.textContent + '\n```\n\n';
var language = node.parentNode.className.match(highlightRegEx)[1]
return '\n\n```' + language + '\n' + node.textContent + '\n```\n\n'
}
},
{
filter: function (node) {
return node.nodeName === 'DIV' &&
highlightRegEx.test(node.className);
highlightRegEx.test(node.className)
},
replacement: function (content) {
return '\n\n' + content + '\n\n';
return '\n\n' + content + '\n\n'
}
}
];
]
},{}],3:[function(require,module,exports){
'use strict';
/*
* Set up window for Node.js
*/
var _window = (typeof window !== 'undefined' ? window : this)
/*
* Parsing HTML strings
*/
function canParseHtmlNatively () {
var Parser = _window.DOMParser
var canParse = false
// Adapted from https://gist.github.com/1129031
// Firefox/Opera/IE throw errors on unsupported types
try {
// WebKit returns null on unsupported types
if (new Parser().parseFromString('', 'text/html')) {
canParse = true
}
} catch (e) {}
return canParse
}
function createHtmlParser () {
var Parser = function () {}
// For Node.js environments
if (typeof document === 'undefined') {
var jsdom = require('jsdom')
Parser.prototype.parseFromString = function (string) {
return jsdom.jsdom(string, {
features: {
FetchExternalResources: [],
ProcessExternalResources: false
}
})
}
} else {
if (!shouldUseActiveX()) {
Parser.prototype.parseFromString = function (string) {
var doc = document.implementation.createHTMLDocument('')
doc.open()
doc.write(string)
doc.close()
return doc
}
} else {
Parser.prototype.parseFromString = function (string) {
var doc = new window.ActiveXObject('htmlfile')
doc.designMode = 'on' // disable on-page scripts
doc.open()
doc.write(string)
doc.close()
return doc
}
}
}
return Parser
}
function shouldUseActiveX () {
var useActiveX = false
try {
document.implementation.createHTMLDocument('').open()
} catch (e) {
if (window.ActiveXObject) useActiveX = true
}
return useActiveX
}
module.exports = canParseHtmlNatively() ? _window.DOMParser : createHtmlParser()
},{"jsdom":6}],4:[function(require,module,exports){
'use strict'
module.exports = [
{
filter: 'p',
replacement: function (content) {
return '\n\n' + content + '\n\n';
}
},
{
filter: 'div',
replacement: function (content) {
return content + '\n';
return '\n\n' + content + '\n\n'
}
},
{
filter: 'br',
replacement: function () {
return ' \n';
return ' \n'
}
},
{
filter: ['h1', 'h2', 'h3', 'h4', 'h5', 'h6'],
replacement: function (content, node) {
var hLevel = node.nodeName.charAt(1);
var hPrefix = '';
var hLevel = node.nodeName.charAt(1)
var hPrefix = ''
for (var i = 0; i < hLevel; i++) {
hPrefix += '#';
hPrefix += '#'
}
return '\n\n' + hPrefix + ' ' + content + '\n\n';
return '\n\n' + hPrefix + ' ' + content + '\n\n'
}
},
{
filter: 'hr',
replacement: function () {
return '\n\n* * *\n\n';
return '\n\n* * *\n\n'
}
},
{
filter: ['em', 'i'],
replacement: function (content) {
return '*' + content + '*';
return '_' + content + '_'
}
},
{
filter: ['strong', 'b'],
replacement: function (content) {
return '**' + content + '**'
}
},
{
filter: ['u'],
replacement: function (content) {
return '_' + content + '_';
return '_' + content + '_'
}
},
{
filter: ['strong', 'b'],
filter: ['del', 's', 'strike'],
replacement: function (content) {
return '**' + content + '**';
return '~~' + content + '~~'
}
},
{
filter: ['strike','s'],
filter: 'div',
replacement: function (content) {
return '~~' + content + '~~';
return content + '\n\n'
}
},
// Inline code
{
filter: function (node) {
var hasSiblings = node.previousSibling || node.nextSibling;
var isCodeBlock = node.parentNode.nodeName === 'PRE' && !hasSiblings;
var hasSiblings = node.previousSibling || node.nextSibling
var isCodeBlock = node.parentNode.nodeName === 'PRE' && !hasSiblings
return node.nodeName === 'CODE' && !isCodeBlock;
return node.nodeName === 'CODE' && !isCodeBlock
},
replacement: function (content) {
return '`' + content + '`';
return '`' + content + '`'
}
},
{
filter: function (node) {
return node.nodeName === 'A' && node.getAttribute('href');
return node.nodeName === 'A' && node.getAttribute('href')
},
replacement: function (content, node) {
var titlePart = node.title ? ' "'+ node.title +'"' : '';
return '[' + content + '](' + node.getAttribute('href') + titlePart + ')';
var titlePart = node.title ? ' "' + node.title + '"' : ''
return '[' + content + '](' + node.getAttribute('href') + titlePart + ')'
}
},
{
filter: 'video',
replacement: function (content, node) {
var alt = node.getAttribute("alt") || '';
var src ;
for (var i = 0; i < node.childNodes.length; i++)
{
if (node.childNodes[i].localName == 'source') {
src = node.childNodes[i].getAttribute('src') ;
break;
}
}
var title = node.title || '';
var titlePart = title ? ' "'+ title +'"' : '';
return src ? '![video:' + alt + ']' + '(' + src + titlePart + ')' : '';
var alt = node.getAttribute('alt') || ''
var src
for (var i = 0; i < node.childNodes.length; i++) {
if (node.childNodes[i].localName === 'source') {
src = node.childNodes[i].getAttribute('src')
break
} }
var title = node.title || ''
var titlePart = title ? ' "' + title + '"' : ''
return src ? '![video:' + alt + ']' + '(' + src + titlePart + ')' : ''
}
},
{
filter: 'audio',
replacement: function (content, node) {
var alt = node.getAttribute("alt") || '';
var src = node.getAttribute('src') || '';
if (!src)
for (var i = 0; i < node.childNodes.length; i++)
{
if (node.childNodes[i].localName == 'source') {
src = node.childNodes[i].getAttribute('src') ;
break;
}
}
var title = node.title || '';
var titlePart = title ? ' "'+ title +'"' : '';
return src ? '![audio:' + alt + ']' + '(' + src + titlePart + ')' : '';
var alt = node.getAttribute('alt') || ''
var src = node.getAttribute('src') || ''
if (!src) {
for (var i = 0; i < node.childNodes.length; i++) {
if (node.childNodes[i].localName === 'source') {
src = node.childNodes[i].getAttribute('src')
break
} } }
var title = node.title || ''
var titlePart = title ? ' "' + title + '"' : ''
return src ? '![audio:' + alt + ']' + '(' + src + titlePart + ')' : ''
}
},
{
filter: 'img',
replacement: function (content, node) {
var alt = node.getAttribute("alt") || '';
var src = node.getAttribute('src') || '';
var title = node.getAttribute('title') || '';
var titlePart = title ? ' "'+ title +'"' : '';
return src ? '![' + alt + ']' + '(' + src + titlePart + ')' : '';
var alt = node.alt || ''
var src = node.getAttribute('src') || ''
var title = node.title || ''
var titlePart = title ? ' "' + title + '"' : ''
return src ? '![' + alt + ']' + '(' + src + titlePart + ')' : ''
}
},
// Code blocks
{
filter: function (node) {
return node.nodeName === 'PRE' && node.firstChild.nodeName === 'CODE';
return node.nodeName === 'PRE' && node.firstChild.nodeName === 'CODE'
},
replacement: function (content, node) {
return '\n\n ' + node.firstChild.textContent.replace(/\n/g, '\n ') + '\n\n';
return '\n\n ' + node.firstChild.textContent.replace(/\n/g, '\n ') + '\n\n'
}
},
{
filter: 'blockquote',
replacement: function (content) {
content = this.trim(content);
content = content.replace(/\n{3,}/g, '\n\n');
content = content.replace(/^/gm, '> ');
return '\n\n' + content + '\n\n';
content = content.trim()
content = content.replace(/\n{3,}/g, '\n\n')
content = content.replace(/^/gm, '> ')
return '\n\n' + content + '\n\n'
}
},
{
filter: 'li',
replacement: function (content, node) {
content = content.replace(/^\s+/, '').replace(/\n/gm, '\n ');
var prefix = '* ';
var parent = node.parentNode;
var index = Array.prototype.indexOf.call(parent.children, node) + 1;
content = content.replace(/^\s+/, '').replace(/\n/gm, '\n ')
var prefix = '* '
var parent = node.parentNode
var index = Array.prototype.indexOf.call(parent.children, node) + 1
prefix = /ol/i.test(parent.nodeName) ? index + '. ' : '* ';
return prefix + content;
prefix = /ol/i.test(parent.nodeName) ? index + '. ' : '* '
return prefix + content
}
},
{
filter: ['ul', 'ol'],
replacement: function (content, node) {
var strings = [];
var strings = []
for (var i = 0; i < node.childNodes.length; i++) {
strings.push(node.childNodes[i]._replacement);
strings.push(node.childNodes[i]._replacement)
}
if (/li/i.test(node.parentNode.nodeName)) {
return '\n' + strings.join('\n');
return '\n' + strings.join('\n')
}
return '\n\n' + strings.join('\n') + '\n\n';
return '\n\n' + strings.join('\n') + '\n\n'
}
},
{
filter: function (node) {
return this.isBlock(node);
return this.isBlock(node)
},
replacement: function (content, node) {
return '\n\n' + this.outer(node, content) + '\n\n';
return '\n\n' + this.outer(node, content) + '\n\n'
}
},
// Anything else!
{
filter: function () {
return true;
return true
},
replacement: function (content, node) {
return this.outer(node, content);
return this.outer(node, content)
}
}
]
},{}],5:[function(require,module,exports){
/**
* This file automatically generated from `build.js`.
* Do not manually edit.
*/
module.exports = [
"address",
"article",
"aside",
"audio",
"blockquote",
"canvas",
"dd",
"div",
"dl",
"fieldset",
"figcaption",
"figure",
"footer",
"form",
"h1",
"h2",
"h3",
"h4",
"h5",
"h6",
"header",
"hgroup",
"hr",
"main",
"nav",
"noscript",
"ol",
"output",
"p",
"pre",
"section",
"table",
"tfoot",
"ul",
"video"
];
},{}],4:[function(require,module,exports){
},{}],6:[function(require,module,exports){
},{}],7:[function(require,module,exports){
'use strict';
var voidElements = require('void-elements');
@ -738,51 +813,7 @@ function next(prev, current) {
module.exports = collapseWhitespace;
},{"block-elements":5,"void-elements":6}],5:[function(require,module,exports){
/**
* This file automatically generated from `build.js`.
* Do not manually edit.
*/
module.exports = [
"address",
"article",
"aside",
"audio",
"blockquote",
"canvas",
"dd",
"div",
"dl",
"fieldset",
"figcaption",
"figure",
"footer",
"form",
"h1",
"h2",
"h3",
"h4",
"h5",
"h6",
"header",
"hgroup",
"hr",
"main",
"nav",
"noscript",
"ol",
"output",
"p",
"pre",
"section",
"table",
"tfoot",
"ul",
"video"
];
},{}],6:[function(require,module,exports){
},{"block-elements":5,"void-elements":8}],8:[function(require,module,exports){
/**
* This file automatically generated from `pre-publish.js`.
* Do not manually edit.
@ -807,7 +838,5 @@ module.exports = {
"wbr": true
};
},{}],7:[function(require,module,exports){
},{}]},{},[1])(1)
});
Loading…