Skip to content
This repository has been archived by the owner on Jun 6, 2021. It is now read-only.

Commit

Permalink
Added syntax highlighting to preview & PDF
Browse files Browse the repository at this point in the history
  • Loading branch information
BlackDragonBE committed Apr 5, 2018
1 parent fcf8a3d commit 0ae0b6e
Show file tree
Hide file tree
Showing 98 changed files with 8,382 additions and 19 deletions.
14 changes: 7 additions & 7 deletions MarkdownToRW/MarkdownConverter/DragonConverter/Converter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,16 @@ public static string ConvertMarkdownStringToHtml(string markdown, ConverterOptio
string output = Markdown.ToHtml(markdown, pipeline);
//Console.WriteLine(output);



// HTML readability improvements & RW specific changes

// Code
output = output.Replace("<pre><code class=", "\r\n<pre lang=");
output = output.Replace("lang-", "");
output = output.Replace("language-", "");
output = output.Replace("</code></pre>", "</pre>\r\n");
if (!prepareForPreview)
{
output = output.Replace("<pre><code class=", "\r\n<pre lang=");
output = output.Replace("lang-", "");
output = output.Replace("language-", "");
output = output.Replace("</code></pre>", "</pre>\r\n");
}

// Add attributes
AddClassToImages(ref output, options.FirstImageIsAlignedRight, rootPath);
Expand Down Expand Up @@ -68,7 +69,6 @@ public static string ConvertMarkdownStringToHtml(string markdown, ConverterOptio
// Spoiler
ConvertSpoilers(ref output);


if (options.ReplaceImageWithAltWithCaption)
{
ConvertImagesWithAltToCaptions(ref output);
Expand Down
13 changes: 11 additions & 2 deletions MarkdownToRW/MarkdownConverter/DragonConverter/PdfConverter.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using DinkToPdf;
using DinkToPdf.EventDefinitions;
using DragonMarkdown.Utility;

namespace DragonMarkdown.DragonConverter
{
Expand All @@ -22,19 +24,26 @@ public static void ConvertToPdf(string html, string ouputPath)
ColorMode = ColorMode.Color,
Orientation = Orientation.Portrait,
PaperSize = PaperKind.A4Plus,
Out = ouputPath,
},
Objects = {
new ObjectSettings() {
PagesCount = true,
HtmlContent = html,
WebSettings = { DefaultEncoding = "utf-8" },
HeaderSettings = { FontSize = 9, Left = "My Amazing Title", Right = "Page [page] of [toPage]", Line = true, Spacing = 2.812 },
FooterSettings = { FontSize = 9, Left = "My Amazing Title", Right = "Page [page] of [toPage]", Line = true, Spacing = 2.812 },
}
}
};

converter.Convert(doc);
Console.WriteLine("Saving PDF to " + ouputPath);

byte[] pdf = converter.Convert(doc);

using (FileStream stream = new FileStream(ouputPath, FileMode.Create))
{
stream.Write(pdf, 0, pdf.Length);
}
}

private static void ConverterOnProgressChanged(object sender, ProgressChangedArgs progressChangedArgs)
Expand Down
9 changes: 9 additions & 0 deletions MarkdownToRW/MarkdownConverter/DragonMarkdown.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,15 @@
</ItemGroup>

<ItemGroup>
<None Update="js_highlight\highlight.pack.js">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="js_highlight\styles\atom-one-light.css">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="js_highlight\styles\default.css">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="libwkhtmltox.dll">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
Expand Down
38 changes: 33 additions & 5 deletions MarkdownToRW/MarkdownConverter/PreviewCreator.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Net;
Expand All @@ -12,16 +13,16 @@ namespace DragonMarkdown
{
public class PreviewCreator
{
public static string CreateHtmlPreviewFromMarkdown(string markdown)
public static string CreateHtmlPreviewFromMarkdown(string markdown, string markdownFolder)
{
string output = "";

output = PrepareHtmlForPreview(Converter.ConvertMarkdownStringToHtml(markdown, prepareForPreview: true));
output = PrepareHtmlForPreview(Converter.ConvertMarkdownStringToHtml(markdown, prepareForPreview: true), markdownFolder);

return output;
}

private static string PrepareHtmlForPreview(string html)
private static string PrepareHtmlForPreview(string html, string markdownFolder)
{
string output = html;

Expand All @@ -31,6 +32,17 @@ private static string PrepareHtmlForPreview(string html)
output += "</div></body></html>";

FixCode(ref output);
ReplaceImageSources(ref output, markdownFolder);

// Syntax Highlighting
//output = output.Replace("!!!DEFAULT_CSS_PATH!!!", "file://" + Path.GetFullPath("js_highlight/styles/default.css"));
output = output.Replace("!!!DEFAULT_CSS_PATH!!!", "file://" + Path.GetFullPath("js_highlight/styles/atom-one-light.css"));
output = output.Replace("!!!PACK_JS_PATH!!!", "file://" + Path.GetFullPath("js_highlight/highlight.pack.js"));

// PDF
output = output.Replace("$[=p=]", "<div class=\"pb_after\"></div>");
output = output.Replace("$[===]", "");
output = output.Replace("$[=s=]", "");

return output;
}
Expand All @@ -53,15 +65,31 @@ private static void FixCode(ref string html)
codeNode.ReplaceChildHtml(newHtml);
}
}

html = doc.DocumentNode.OuterHtml;
}

private static void ReplaceImageSources(ref string html, string folder)
{
var imageData = Converter.FindAllImageLinksInHtml(html, folder);

List<string> localPaths = new List<string>();
List<string> fullPaths = new List<string>();

foreach (ImageLinkData data in imageData)
{
localPaths.Add(data.LocalImagePath);
fullPaths.Add(data.FullImagePath);
}

html = DragonUtil.BatchReplaceText(html, localPaths, fullPaths);
}

public static void CreateHtmlPreviewFileFromMarkdown(string markdown, string filePath)
{
using (StreamWriter sw = new StreamWriter(filePath))
{
sw.Write(CreateHtmlPreviewFromMarkdown(markdown));
sw.Write(CreateHtmlPreviewFromMarkdown(markdown, Path.GetDirectoryName(filePath)));
sw.Flush();
sw.Close();
}
Expand Down
15 changes: 15 additions & 0 deletions MarkdownToRW/MarkdownConverter/Utility/PreviewHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,25 @@ public static class PreviewHelper
public static string css = "<!DOCTYPE html>" +
"<html>" +
"<head>" +
"<link rel=\"stylesheet\" href=\"!!!DEFAULT_CSS_PATH!!!\">" +
"<script src=\"!!!PACK_JS_PATH!!!\"></script>" +
"<script>hljs.configure(); hljs.initHighlightingOnLoad();</script>" +
"<style>" +
"* {" +
" font-size: 100%;" +
" font-family: Bitter,Georgia,serif" +
"}" +
"/* Page Breaks */" +
"/***Always insert a page break before the element***/" +
".pb_before {page-break-before: always !important;}" +
"/***Always insert a page break after the element***/" +
".pb_after {page-break-after: always !important;}" +
"/***Avoid page break before the element (if possible)***/" +
".pb_before_avoid {page-break-before: avoid !important;}" +
"/***Avoid page break after the element (if possible)***/" +
".pb_after_avoid {page-break-after: avoid !important;}" +
"/* Avoid page break inside the element (if possible) */" +
".pbi_avoid {page-break-inside: avoid !important;}" +
"div.content { width: 860px }" +
"img {" +
" display: block;" +
Expand Down Expand Up @@ -58,4 +72,5 @@ public static class PreviewHelper
"<body>" +
"<div class=\"content\">";
}

}
Binary file removed MarkdownToRW/MarkdownConverter/WordPressSharp.zip
Binary file not shown.
Binary file removed MarkdownToRW/MarkdownConverter/XmlRpcLight.zip
Binary file not shown.
Loading

0 comments on commit 0ae0b6e

Please sign in to comment.