rudimentary image support
This commit is contained in:
parent
8f89d1e951
commit
8fd43e624c
|
@ -15,6 +15,7 @@
|
||||||
string tempdir = null;
|
string tempdir = null;
|
||||||
string format = null;
|
string format = null;
|
||||||
string author = null;
|
string author = null;
|
||||||
|
string images = null;
|
||||||
var debug = false;
|
var debug = false;
|
||||||
|
|
||||||
if (args.Length == 1)
|
if (args.Length == 1)
|
||||||
|
@ -45,6 +46,9 @@
|
||||||
case "--author":
|
case "--author":
|
||||||
author = args[i + 1];
|
author = args[i + 1];
|
||||||
break;
|
break;
|
||||||
|
case "--images":
|
||||||
|
images = args[i + 1];
|
||||||
|
break;
|
||||||
case "--debug":
|
case "--debug":
|
||||||
i--;
|
i--;
|
||||||
debug = true;
|
debug = true;
|
||||||
|
@ -96,6 +100,12 @@
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!string.IsNullOrWhiteSpace(images) && !Directory.Exists(images))
|
||||||
|
{
|
||||||
|
Console.WriteLine(@"Images directory {0} does not exist.", images);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
var parser = new FicdownParser();
|
var parser = new FicdownParser();
|
||||||
var storyText = File.ReadAllText(infile);
|
var storyText = File.ReadAllText(infile);
|
||||||
|
|
||||||
|
@ -108,11 +118,7 @@
|
||||||
{
|
{
|
||||||
case "html":
|
case "html":
|
||||||
Directory.CreateDirectory(output);
|
Directory.CreateDirectory(output);
|
||||||
rend = (string.IsNullOrWhiteSpace(tempdir)
|
rend = new HtmlRenderer();
|
||||||
? new HtmlRenderer()
|
|
||||||
: new HtmlRenderer(File.ReadAllText(Path.Combine(tempdir, "index.html")),
|
|
||||||
File.ReadAllText(Path.Combine(tempdir, "scene.html")),
|
|
||||||
File.ReadAllText(Path.Combine(tempdir, "styles.css"))));
|
|
||||||
break;
|
break;
|
||||||
case "epub":
|
case "epub":
|
||||||
if (string.IsNullOrWhiteSpace(author))
|
if (string.IsNullOrWhiteSpace(author))
|
||||||
|
@ -120,17 +126,22 @@
|
||||||
Console.WriteLine(@"Epub format requires the --author argument.");
|
Console.WriteLine(@"Epub format requires the --author argument.");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
rend = (string.IsNullOrWhiteSpace(tempdir)
|
rend = new EpubRenderer(author);
|
||||||
? new EpubRenderer(author)
|
|
||||||
: new EpubRenderer(author, File.ReadAllText(Path.Combine(tempdir, "index.html")),
|
|
||||||
File.ReadAllText(Path.Combine(tempdir, "scene.html")),
|
|
||||||
File.ReadAllText(Path.Combine(tempdir, "styles.css"))));
|
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
ShowHelp();
|
ShowHelp();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!string.IsNullOrWhiteSpace(tempdir))
|
||||||
|
{
|
||||||
|
rend.IndexTemplate = File.ReadAllText(Path.Combine(tempdir, "index.html"));
|
||||||
|
rend.SceneTemplate = File.ReadAllText(Path.Combine(tempdir, "scene.html"));
|
||||||
|
rend.StylesTemplate = File.ReadAllText(Path.Combine(tempdir, "styles.css"));
|
||||||
|
};
|
||||||
|
|
||||||
|
if (!string.IsNullOrWhiteSpace(images)) rend.ImageDir = images;
|
||||||
|
|
||||||
Console.WriteLine(@"Rendering story...");
|
Console.WriteLine(@"Rendering story...");
|
||||||
|
|
||||||
rend.Render(story, output, debug);
|
rend.Render(story, output, debug);
|
||||||
|
@ -138,6 +149,7 @@
|
||||||
Console.WriteLine(@"Done.");
|
Console.WriteLine(@"Done.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private static void ShowHelp()
|
private static void ShowHelp()
|
||||||
{
|
{
|
||||||
Console.WriteLine(
|
Console.WriteLine(
|
||||||
|
@ -146,7 +158,8 @@
|
||||||
--in ""/path/to/source.md""
|
--in ""/path/to/source.md""
|
||||||
[--out ""/path/to/output""]
|
[--out ""/path/to/output""]
|
||||||
[--template ""/path/to/template/dir""]
|
[--template ""/path/to/template/dir""]
|
||||||
[--author ""Author Name""
|
[--images ""/path/to/images/dir""]
|
||||||
|
[--author ""Author Name""]
|
||||||
[--debug]");
|
[--debug]");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -76,6 +76,7 @@
|
||||||
<Compile Include="Render\EpubRenderer.cs" />
|
<Compile Include="Render\EpubRenderer.cs" />
|
||||||
<Compile Include="Render\HtmlRenderer.cs" />
|
<Compile Include="Render\HtmlRenderer.cs" />
|
||||||
<Compile Include="Render\IRenderer.cs" />
|
<Compile Include="Render\IRenderer.cs" />
|
||||||
|
<Compile Include="Render\MimeHelper.cs" />
|
||||||
<Compile Include="Render\Template.Designer.cs">
|
<Compile Include="Render\Template.Designer.cs">
|
||||||
<AutoGen>True</AutoGen>
|
<AutoGen>True</AutoGen>
|
||||||
<DesignTime>True</DesignTime>
|
<DesignTime>True</DesignTime>
|
||||||
|
|
|
@ -15,11 +15,6 @@
|
||||||
_author = author;
|
_author = author;
|
||||||
}
|
}
|
||||||
|
|
||||||
public EpubRenderer(string author, string index, string scene, string styles) : base(index, scene, styles)
|
|
||||||
{
|
|
||||||
_author = author;
|
|
||||||
}
|
|
||||||
|
|
||||||
public override void Render(Model.Parser.ResolvedStory story, string outPath, bool debug = false)
|
public override void Render(Model.Parser.ResolvedStory story, string outPath, bool debug = false)
|
||||||
{
|
{
|
||||||
var temppath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
|
var temppath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
|
||||||
|
@ -39,6 +34,18 @@
|
||||||
var epub = new Epub(Story.Name, _author, chapters);
|
var epub = new Epub(Story.Name, _author, chapters);
|
||||||
epub.AddResourceFile(new ResourceFile("styles.css", Path.Combine(temppath, "styles.css"), "text/css"));
|
epub.AddResourceFile(new ResourceFile("styles.css", Path.Combine(temppath, "styles.css"), "text/css"));
|
||||||
|
|
||||||
|
if (!string.IsNullOrWhiteSpace(ImageDir))
|
||||||
|
{
|
||||||
|
var dirname = ImageDir.Substring(ImageDir.LastIndexOf(Path.DirectorySeparatorChar) + 1);
|
||||||
|
var tempimgdir = Path.Combine(temppath, dirname);
|
||||||
|
foreach (var img in Directory.GetFiles(tempimgdir))
|
||||||
|
{
|
||||||
|
var fname = Path.GetFileName(img);
|
||||||
|
epub.AddResourceFile(new ResourceFile(fname,
|
||||||
|
Path.Combine(tempimgdir, fname), MimeHelper.GetMimeType(img)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
var builder = new EPubBuilder();
|
var builder = new EPubBuilder();
|
||||||
var built = builder.Build(epub);
|
var built = builder.Build(epub);
|
||||||
|
|
||||||
|
|
|
@ -11,25 +11,16 @@
|
||||||
{
|
{
|
||||||
protected readonly Markdown Markdown;
|
protected readonly Markdown Markdown;
|
||||||
|
|
||||||
private string _index;
|
public string IndexTemplate { get; set; }
|
||||||
private string _scene;
|
public string SceneTemplate { get; set; }
|
||||||
private string _styles;
|
public string StylesTemplate { get; set; }
|
||||||
|
public string ImageDir { get; set; }
|
||||||
|
|
||||||
protected ResolvedStory Story { get; set; }
|
protected ResolvedStory Story { get; set; }
|
||||||
|
|
||||||
public HtmlRenderer()
|
public HtmlRenderer()
|
||||||
{
|
{
|
||||||
Markdown = new Markdown();
|
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)
|
public virtual void Render(ResolvedStory story, string outPath, bool debug = false)
|
||||||
|
@ -46,7 +37,7 @@
|
||||||
|
|
||||||
protected void GenerateHtml(ResolvedStory story, string outPath, bool debug)
|
protected void GenerateHtml(ResolvedStory story, string outPath, bool debug)
|
||||||
{
|
{
|
||||||
var index = FillTemplate(_index, new Dictionary<string, string>
|
var index = FillTemplate(IndexTemplate ?? Template.Index, new Dictionary<string, string>
|
||||||
{
|
{
|
||||||
{"Title", story.Name},
|
{"Title", story.Name},
|
||||||
{"Description", Markdown.Transform(story.Description)},
|
{"Description", Markdown.Transform(story.Description)},
|
||||||
|
@ -57,7 +48,7 @@
|
||||||
|
|
||||||
foreach (var page in story.Pages)
|
foreach (var page in story.Pages)
|
||||||
{
|
{
|
||||||
File.WriteAllText(Path.Combine(outPath, "styles.css"), _styles);
|
File.WriteAllText(Path.Combine(outPath, "styles.css"), StylesTemplate ?? Template.Styles);
|
||||||
|
|
||||||
var content = page.Content;
|
var content = page.Content;
|
||||||
foreach (var anchor in Utilities.ParseAnchors(page.Content))
|
foreach (var anchor in Utilities.ParseAnchors(page.Content))
|
||||||
|
@ -71,7 +62,7 @@
|
||||||
string.Join("\n", page.ActiveToggles.Select(t => string.Format("- {0}", t)).ToArray()));
|
string.Join("\n", page.ActiveToggles.Select(t => string.Format("- {0}", t)).ToArray()));
|
||||||
}
|
}
|
||||||
|
|
||||||
var scene = FillTemplate(_scene, new Dictionary<string, string>
|
var scene = FillTemplate(SceneTemplate ?? Template.Scene, new Dictionary<string, string>
|
||||||
{
|
{
|
||||||
{"Title", story.Name},
|
{"Title", story.Name},
|
||||||
{"Content", Markdown.Transform(content)}
|
{"Content", Markdown.Transform(content)}
|
||||||
|
@ -79,6 +70,22 @@
|
||||||
|
|
||||||
File.WriteAllText(Path.Combine(outPath, string.Format("{0}.html", page.Name)), scene);
|
File.WriteAllText(Path.Combine(outPath, string.Format("{0}.html", page.Name)), scene);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,9 +1,14 @@
|
||||||
namespace Ficdown.Parser.Render
|
namespace Ficdown.Parser.Render
|
||||||
{
|
{
|
||||||
|
using System.Security.Cryptography.X509Certificates;
|
||||||
using Model.Parser;
|
using Model.Parser;
|
||||||
|
|
||||||
public interface IRenderer
|
public interface IRenderer
|
||||||
{
|
{
|
||||||
|
string IndexTemplate { get; set; }
|
||||||
|
string SceneTemplate { get; set; }
|
||||||
|
string StylesTemplate { get; set; }
|
||||||
|
string ImageDir { get; set; }
|
||||||
void Render(ResolvedStory story, string outPath, bool debug);
|
void Render(ResolvedStory story, string outPath, bool debug);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,25 @@
|
||||||
|
namespace Ficdown.Parser.Render
|
||||||
|
{
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.IO;
|
||||||
|
|
||||||
|
public static class MimeHelper
|
||||||
|
{
|
||||||
|
private static Dictionary<string, string> _mimeTypes = new Dictionary<string, string>
|
||||||
|
{
|
||||||
|
{".jpg", "image/jpeg"},
|
||||||
|
{".jpeg", "image/jpeg"},
|
||||||
|
{".png", "image/png"},
|
||||||
|
{".gif", "image/gif"},
|
||||||
|
{".bmp", "image/bmp"},
|
||||||
|
{".tif", "image/tiff"},
|
||||||
|
{".tiff", "image/tiff"}
|
||||||
|
};
|
||||||
|
|
||||||
|
public static string GetMimeType(string fileName)
|
||||||
|
{
|
||||||
|
var ext = Path.GetExtension(fileName).ToLower();
|
||||||
|
return !_mimeTypes.ContainsKey(ext) ? "application/octet-stream" : _mimeTypes[ext];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue