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;
|
|
|
|
|
|
|
|
|
|
private string _index;
|
|
|
|
|
private string _scene;
|
|
|
|
|
private string _styles;
|
|
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
_index = Template.Index;
|
|
|
|
|
_scene = Template.Scene;
|
|
|
|
|
_styles = Template.Styles;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public HtmlRenderer(string index, string scene, string styles)
|
|
|
|
|
{
|
|
|
|
|
_index = index;
|
|
|
|
|
_scene = scene;
|
|
|
|
|
_styles = styles;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
{
|
|
|
|
|
var index = FillTemplate(_index, new Dictionary<string, string>
|
|
|
|
|
{
|
|
|
|
|
{"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-02-08 19:30:12 -06:00
|
|
|
|
File.WriteAllText(Path.Combine(outPath, "styles.css"), _styles);
|
|
|
|
|
|
2014-08-10 11:10:21 -05:00
|
|
|
|
var content = page.Content;
|
|
|
|
|
foreach (var anchor in Utilities.ParseAnchors(page.Content))
|
|
|
|
|
{
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
var scene = FillTemplate(_scene, new Dictionary<string, string>
|
|
|
|
|
{
|
|
|
|
|
{"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
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|