2014-08-10 11:10:21 -05:00
|
|
|
|
namespace Ficdown.Parser.Render
|
|
|
|
|
{
|
2015-02-08 19:30:12 -06:00
|
|
|
|
using System.Collections.Generic;
|
2014-08-10 11:10:21 -05:00
|
|
|
|
using System.IO;
|
2014-08-10 15:25:20 -05:00
|
|
|
|
using System.Linq;
|
2014-08-10 11:10:21 -05:00
|
|
|
|
using MarkdownSharp;
|
|
|
|
|
using Model.Parser;
|
|
|
|
|
using Parser;
|
|
|
|
|
|
2015-02-08 19:30:12 -06:00
|
|
|
|
public class HtmlRenderer : IRenderer
|
2014-08-10 11:10:21 -05:00
|
|
|
|
{
|
2015-02-08 19:30:12 -06:00
|
|
|
|
protected readonly Markdown Markdown;
|
|
|
|
|
|
2015-04-01 17:17:36 -05:00
|
|
|
|
public string IndexTemplate { get; set; }
|
|
|
|
|
public string SceneTemplate { get; set; }
|
|
|
|
|
public string StylesTemplate { get; set; }
|
|
|
|
|
public string ImageDir { get; set; }
|
2015-02-08 19:30:12 -06:00
|
|
|
|
|
|
|
|
|
protected ResolvedStory Story { get; set; }
|
2014-08-10 11:10:21 -05:00
|
|
|
|
|
|
|
|
|
public HtmlRenderer()
|
|
|
|
|
{
|
2015-02-08 19:30:12 -06:00
|
|
|
|
Markdown = new Markdown();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public virtual void Render(ResolvedStory story, string outPath, bool debug = false)
|
|
|
|
|
{
|
|
|
|
|
Story = story;
|
|
|
|
|
GenerateHtml(story, outPath, debug);
|
2014-08-10 11:10:21 -05:00
|
|
|
|
}
|
|
|
|
|
|
2015-02-08 19:30:12 -06:00
|
|
|
|
private string FillTemplate(string template, Dictionary<string, string> values)
|
2014-08-10 11:10:21 -05:00
|
|
|
|
{
|
2015-02-08 19:30:12 -06:00
|
|
|
|
return values.Aggregate(template,
|
|
|
|
|
(current, pair) => current.Replace(string.Format("@{0}", pair.Key), pair.Value));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected void GenerateHtml(ResolvedStory story, string outPath, bool debug)
|
|
|
|
|
{
|
2015-04-01 17:17:36 -05:00
|
|
|
|
var index = FillTemplate(IndexTemplate ?? Template.Index, new Dictionary<string, string>
|
2015-02-08 19:30:12 -06:00
|
|
|
|
{
|
|
|
|
|
{"Title", story.Name},
|
|
|
|
|
{"Description", Markdown.Transform(story.Description)},
|
|
|
|
|
{"FirstScene", string.Format("{0}.html", story.FirstPage)}
|
|
|
|
|
});
|
2014-08-10 17:32:13 -05:00
|
|
|
|
|
2015-02-08 19:30:12 -06:00
|
|
|
|
File.WriteAllText(Path.Combine(outPath, "index.html"), index);
|
2014-08-10 17:32:13 -05:00
|
|
|
|
|
|
|
|
|
foreach (var page in story.Pages)
|
2014-08-10 11:10:21 -05:00
|
|
|
|
{
|
2015-04-01 17:17:36 -05:00
|
|
|
|
File.WriteAllText(Path.Combine(outPath, "styles.css"), StylesTemplate ?? Template.Styles);
|
2015-02-08 19:30:12 -06:00
|
|
|
|
|
2014-08-10 11:10:21 -05:00
|
|
|
|
var content = page.Content;
|
2015-07-19 15:51:10 -05:00
|
|
|
|
foreach (var anchor in Utilities.GetInstance(page.Name).ParseAnchors(page.Content))
|
2014-08-10 11:10:21 -05:00
|
|
|
|
{
|
|
|
|
|
var newAnchor = string.Format("[{0}]({1}.html)", anchor.Text, anchor.Href.Target);
|
|
|
|
|
content = content.Replace(anchor.Original, newAnchor);
|
|
|
|
|
}
|
2014-08-10 15:25:20 -05:00
|
|
|
|
if (debug)
|
|
|
|
|
{
|
|
|
|
|
content += string.Format("\n\n### State Debug\n\n{0}",
|
|
|
|
|
string.Join("\n", page.ActiveToggles.Select(t => string.Format("- {0}", t)).ToArray()));
|
|
|
|
|
}
|
2015-02-08 19:30:12 -06:00
|
|
|
|
|
2015-04-01 17:17:36 -05:00
|
|
|
|
var scene = FillTemplate(SceneTemplate ?? Template.Scene, new Dictionary<string, string>
|
2015-02-08 19:30:12 -06:00
|
|
|
|
{
|
|
|
|
|
{"Title", story.Name},
|
|
|
|
|
{"Content", Markdown.Transform(content)}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
File.WriteAllText(Path.Combine(outPath, string.Format("{0}.html", page.Name)), scene);
|
2014-08-10 11:10:21 -05:00
|
|
|
|
}
|
2015-04-01 17:17:36 -05:00
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(ImageDir))
|
|
|
|
|
{
|
|
|
|
|
var dirname = ImageDir.Substring(ImageDir.LastIndexOf(Path.DirectorySeparatorChar) + 1);
|
|
|
|
|
Directory.CreateDirectory(Path.Combine(outPath, dirname));
|
|
|
|
|
CopyFilesRecursively(ImageDir, Path.Combine(outPath, dirname));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void CopyFilesRecursively(string sourcePath, string destinationPath)
|
|
|
|
|
{
|
|
|
|
|
foreach (var dirPath in Directory.GetDirectories(sourcePath, "*", SearchOption.AllDirectories))
|
|
|
|
|
Directory.CreateDirectory(dirPath.Replace(sourcePath, destinationPath));
|
|
|
|
|
|
|
|
|
|
foreach (var newPath in Directory.GetFiles(sourcePath, "*.*", SearchOption.AllDirectories))
|
|
|
|
|
File.Copy(newPath, newPath.Replace(sourcePath, destinationPath));
|
2014-08-10 11:10:21 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|