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

Commit

Permalink
Added automatic caption generation & example (hell yeah!)
Browse files Browse the repository at this point in the history
  • Loading branch information
BlackDragonBE committed Mar 26, 2018
1 parent 173cf17 commit 5612d7c
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 16 deletions.
4 changes: 4 additions & 0 deletions ConverterTests/FullTest.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ Lots of formats are supported, including GIFs of course:

![](Images/nigel.gif)

You can add an alt text by writing it between the square brackets. MarkdownToRW will convert these into captions (and the alt is preserved as well):

![The stare of Doge penetrates your soul. Why aren't you doing your work?](Images/doge.jpg)

Make sure to add a separate child folder of the folder your markdown is in, you can easily use subfolders in links and images.
The converter can find all locally sourced images, upload them to WordPress and replace the local paths with the image URLs.

Expand Down
68 changes: 52 additions & 16 deletions MarkdownToRW/MarkdownConverter/DragonConverter/Converter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public static string ConvertMarkdownStringToHtml(string markdown, ConverterOptio

// Spoiler
ConvertSpoilers(ref output);
ConvertImagesWithAltToCaptions(ref output);

// Final cleanup
output = output.Replace("<div></div>", "");
Expand All @@ -73,7 +74,8 @@ public static string ConvertMarkdownStringToHtml(string markdown, ConverterOptio
return output;
}

public static void ConvertMarkdownFileToHtmlFile(string markdownFilePath, string htmlFilePath, ConverterOptions options = null)
public static void ConvertMarkdownFileToHtmlFile(string markdownFilePath, string htmlFilePath,
ConverterOptions options = null)
{
using (StreamReader sr = new StreamReader(markdownFilePath))
{
Expand Down Expand Up @@ -142,6 +144,39 @@ private static void AddExtraAttributesToLinks(ref string html)
html = doc.DocumentNode.OuterHtml;
}

private static void ConvertImagesWithAltToCaptions(ref string html)
{
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(html);
HtmlNodeCollection imgNodes = doc.DocumentNode.SelectNodes("//img");

if (imgNodes == null || imgNodes.Count == 0)
{
return;
}

foreach (HtmlNode imgNode in imgNodes)
{
if (imgNode.Attributes["alt"].Value != "")
{
HtmlNode parent = imgNode.ParentNode;

HtmlDocument newDoc = new HtmlDocument();
HtmlNode newElement = newDoc.CreateElement("caption");
newElement.SetAttributeValue("align", imgNode.Attributes["class"].Value);
newElement.AppendChild(imgNode);
newElement.InnerHtml += imgNode.Attributes["alt"].Value;

parent.ReplaceChild(newElement, imgNode);

ReplaceOuterHtmlWithSquareBrackets(newElement);

}
}

html = doc.DocumentNode.OuterHtml;
}

private static void ConvertSpoilers(ref string html)
{
HtmlDocument doc = new HtmlDocument();
Expand All @@ -162,18 +197,7 @@ private static void ConvertSpoilers(ref string html)
divNode.Attributes.Add("title", spoilerTitle);
divNode.Name = "spoiler";

string inner = divNode.InnerHtml;
string newOuter = divNode.OuterHtml;

newOuter = newOuter.Replace(inner, "");
newOuter = newOuter.Replace("<", "[");
newOuter = newOuter.Replace(">", "]");
//newOuter = newOuter.Replace("INNER", inner);

var newNode = HtmlNode.CreateNode(newOuter);
newNode.InnerHtml = newNode.InnerHtml.Replace("][", "]" + inner.Trim() + "[");
divNode.ParentNode.ReplaceChild(newNode, divNode);

ReplaceOuterHtmlWithSquareBrackets(divNode);
}
}

Expand Down Expand Up @@ -241,15 +265,27 @@ public static string ReplaceLocalImageLinksWithUrls(string markdownPath, string

return markdownText;
}
}

private static void ReplaceOuterHtmlWithSquareBrackets(HtmlNode node)
{
string inner = node.InnerHtml;
string newOuter = node.OuterHtml;

newOuter = newOuter.Replace(inner, "");
newOuter = newOuter.Replace("<", "[");
newOuter = newOuter.Replace(">", "]");
//newOuter = newOuter.Replace("INNER", inner);

var newNode = HtmlNode.CreateNode(newOuter);
newNode.InnerHtml = newNode.InnerHtml.Replace("][", "]" + inner.Trim() + "[");
node.ParentNode.ReplaceChild(newNode, node);
}
}


public struct ImageLinkData
{
public string FullImagePath;
public string LocalImagePath;
}


}

0 comments on commit 5612d7c

Please sign in to comment.