Skip to content

Commit

Permalink
CSS in inserted and linked to now when supplied
Browse files Browse the repository at this point in the history
  • Loading branch information
LeeReid1 committed Dec 29, 2022
1 parent ae00920 commit c04007f
Show file tree
Hide file tree
Showing 20 changed files with 108 additions and 26 deletions.
3 changes: 3 additions & 0 deletions Example_Input/additional-style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
nav-link:hover{
background:aqua
}
3 changes: 2 additions & 1 deletion Example_Output/Index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
<meta property="keywords" content="cats example">
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-rbsA2VBKQhggwzxH7pPCaAqO46MgnOM80zW1RWuH61DGLwZJEdK2Kadq2F9CUG65" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-kenU1KFdBIe4zVF0s0G1M5b4hcpxyD9F7jL+jjXkk+Q2h455rYXK/7HAuoJl+0I4" crossorigin="anonymous"></script>
<link href="/css/additional-style.css" rel="stylesheet">
</head>
<body>
<main id="content" class="bd-content order-1 py-5"><div class="container-xxl bd-gutter">
<nav class="navbar navbar-expand-lg navbar-light bg-light"><div class="container-fluid">
<nav class="navbar navbar-light bg-light"><div class="container-fluid">
<a class="navbar-brand me-auto" style="margin-left: 20px" href="/Index.html">My Generated Site</a>
<button
class="navbar-toggler bg-light"
Expand Down
3 changes: 2 additions & 1 deletion Example_Output/cat-topics/anatomy.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
<html lang="en-nz"><head>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-rbsA2VBKQhggwzxH7pPCaAqO46MgnOM80zW1RWuH61DGLwZJEdK2Kadq2F9CUG65" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-kenU1KFdBIe4zVF0s0G1M5b4hcpxyD9F7jL+jjXkk+Q2h455rYXK/7HAuoJl+0I4" crossorigin="anonymous"></script>
<link href="/css/additional-style.css" rel="stylesheet">
</head>
<body>
<main id="content" class="bd-content order-1 py-5"><div class="container-xxl bd-gutter">
<nav class="navbar navbar-expand-lg navbar-light bg-light"><div class="container-fluid">
<nav class="navbar navbar-light bg-light"><div class="container-fluid">
<a class="navbar-brand me-auto" style="margin-left: 20px" href="/Index.html">My Generated Site</a>
<button
class="navbar-toggler bg-light"
Expand Down
3 changes: 2 additions & 1 deletion Example_Output/cat-topics/behaviour.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
<title>Behaviour and Intelligence</title>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-rbsA2VBKQhggwzxH7pPCaAqO46MgnOM80zW1RWuH61DGLwZJEdK2Kadq2F9CUG65" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-kenU1KFdBIe4zVF0s0G1M5b4hcpxyD9F7jL+jjXkk+Q2h455rYXK/7HAuoJl+0I4" crossorigin="anonymous"></script>
<link href="/css/additional-style.css" rel="stylesheet">
</head>
<body>
<main id="content" class="bd-content order-1 py-5"><div class="container-xxl bd-gutter">
<nav class="navbar navbar-expand-lg navbar-light bg-light"><div class="container-fluid">
<nav class="navbar navbar-light bg-light"><div class="container-fluid">
<a class="navbar-brand me-auto" style="margin-left: 20px" href="/Index.html">My Generated Site</a>
<button
class="navbar-toggler bg-light"
Expand Down
3 changes: 2 additions & 1 deletion Example_Output/cat-topics/cat-care.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
<meta property="thiswill" content="appear in metadata but the title will only appear as <title></title>">
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-rbsA2VBKQhggwzxH7pPCaAqO46MgnOM80zW1RWuH61DGLwZJEdK2Kadq2F9CUG65" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-kenU1KFdBIe4zVF0s0G1M5b4hcpxyD9F7jL+jjXkk+Q2h455rYXK/7HAuoJl+0I4" crossorigin="anonymous"></script>
<link href="/css/additional-style.css" rel="stylesheet">
</head>
<body>
<main id="content" class="bd-content order-1 py-5"><div class="container-xxl bd-gutter">
<nav class="navbar navbar-expand-lg navbar-light bg-light"><div class="container-fluid">
<nav class="navbar navbar-light bg-light"><div class="container-fluid">
<a class="navbar-brand me-auto" style="margin-left: 20px" href="/Index.html">My Generated Site</a>
<button
class="navbar-toggler bg-light"
Expand Down
3 changes: 2 additions & 1 deletion Example_Output/cat-topics/history.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
<title>History</title>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-rbsA2VBKQhggwzxH7pPCaAqO46MgnOM80zW1RWuH61DGLwZJEdK2Kadq2F9CUG65" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-kenU1KFdBIe4zVF0s0G1M5b4hcpxyD9F7jL+jjXkk+Q2h455rYXK/7HAuoJl+0I4" crossorigin="anonymous"></script>
<link href="/css/additional-style.css" rel="stylesheet">
</head>
<body>
<main id="content" class="bd-content order-1 py-5"><div class="container-xxl bd-gutter">
<nav class="navbar navbar-expand-lg navbar-light bg-light"><div class="container-fluid">
<nav class="navbar navbar-light bg-light"><div class="container-fluid">
<a class="navbar-brand me-auto" style="margin-left: 20px" href="/Index.html">My Generated Site</a>
<button
class="navbar-toggler bg-light"
Expand Down
3 changes: 3 additions & 0 deletions Example_Output/css/additional-style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
nav-link:hover{
background:aqua
}
1 change: 1 addition & 0 deletions Example_Output/sitemap.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<title>Site Map</title>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-rbsA2VBKQhggwzxH7pPCaAqO46MgnOM80zW1RWuH61DGLwZJEdK2Kadq2F9CUG65" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-kenU1KFdBIe4zVF0s0G1M5b4hcpxyD9F7jL+jjXkk+Q2h455rYXK/7HAuoJl+0I4" crossorigin="anonymous"></script>
<link href="/css/additional-style.css" rel="stylesheet">
</head>
<body>
<main id="content" class="bd-content order-1 py-5"><div class="container-xxl bd-gutter">
Expand Down
2 changes: 1 addition & 1 deletion Example_Output/sitemap.xml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<urlset><url><loc>https://example.com/cat-topics/anatomy.html</loc><lastmod>2022-12-12</lastmod><priority>1</priority><changefreq>monthly</changefreq></url><url><loc>https://example.com/cat-topics/behaviour.html</loc><lastmod>2022-12-12</lastmod><priority>1</priority><changefreq>monthly</changefreq></url><url><loc>https://example.com/cat-topics/cat-care.html</loc><lastmod>2022-12-12</lastmod><priority>1</priority><changefreq>monthly</changefreq></url><url><loc>https://example.com/cat-topics/history.html</loc><lastmod>2022-12-12</lastmod><priority>1</priority><changefreq>monthly</changefreq></url><url><loc>https://example.com/Index.html</loc><lastmod>2022-12-12</lastmod><priority>1</priority><changefreq>monthly</changefreq></url></urlset>
<urlset><url><loc>https://example.com/cat-topics/anatomy.html</loc><lastmod>2022-12-29</lastmod><priority>1</priority><changefreq>monthly</changefreq></url><url><loc>https://example.com/cat-topics/behaviour.html</loc><lastmod>2022-12-29</lastmod><priority>1</priority><changefreq>monthly</changefreq></url><url><loc>https://example.com/cat-topics/cat-care.html</loc><lastmod>2022-12-29</lastmod><priority>1</priority><changefreq>monthly</changefreq></url><url><loc>https://example.com/cat-topics/history.html</loc><lastmod>2022-12-29</lastmod><priority>1</priority><changefreq>monthly</changefreq></url><url><loc>https://example.com/Index.html</loc><lastmod>2022-12-29</lastmod><priority>1</priority><changefreq>monthly</changefreq></url></urlset>
7 changes: 4 additions & 3 deletions MarkdownToSiteGenerator/Enums.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ namespace MarkdownToSiteGenerator
public enum FileTypes
{
None = 0,
SourceFiles,
Images,
All = SourceFiles| Images,
SourceFiles=0x1,
Images = 0x2,
Style = 0x4,
All = SourceFiles| Images | Style,
}
}
5 changes: 5 additions & 0 deletions MarkdownToSiteGenerator/FileSourceProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ public FilePath[] GetFileLocations(FileTypes sourceFilesOnly)
{
extensions.Add(".md");
}

if(sourceFilesOnly.HasFlag(FileTypes.Style))
{
extensions.Add(".css");
}
if(sourceFilesOnly.HasFlag(FileTypes.Images))
{
extensions.AddRange(ImageSuffixes);
Expand Down
7 changes: 6 additions & 1 deletion MarkdownToSiteGenerator/FileWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@ public void Delete(FilePath path)
public async Task Write(StringBuilder content, FilePath destination)
{
string dest = Write_Prepare(destination);

using StreamWriter stream = new(dest, false, Encoding.UTF8, 128000);
await stream.WriteAsync(content);
}
public async Task Write(string content, FilePath destination)
{
string dest = Write_Prepare(destination);
using StreamWriter stream = new(dest, false, Encoding.UTF8, 128000);
await stream.WriteAsync(content);
}
Expand Down
14 changes: 11 additions & 3 deletions MarkdownToSiteGenerator/HTML/HTMLGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,18 @@ public HTMLGenerator(SymbolisedDocument doc, Configuration config)
this.config = config;
}

public StringBuilder Generate(Func<string, string> linkRewriter, ICollection<(string url, string title)>? itemsInNavBar = null)
public StringBuilder Generate(Func<string, string> linkRewriter, ICollection<(string url, string title)>? itemsInNavBar = null, IEnumerable<string>? styleURLs=null)
{
HTML.HtmlDocument htmlDoc = (HtmlDocument)ToHTMLSymbols(doc, doc.Source, linkRewriter);
AddOptionalsToDoc(config, itemsInNavBar, htmlDoc, linkRewriter);
AddOptionalsToDoc(config, itemsInNavBar, htmlDoc, linkRewriter, styleURLs);

return htmlDoc.Write(new StringBuilder());
}

/// <summary>
/// Adds things other than content to the doc, like the navigation bar and styling
/// </summary>
internal static void AddOptionalsToDoc(Configuration config, ICollection<(string url, string title)>? itemsInNavBar, HtmlDocument htmlDoc, Func<string, string> linkRewriter)
internal static void AddOptionalsToDoc(Configuration config, ICollection<(string url, string title)>? itemsInNavBar, HtmlDocument htmlDoc, Func<string, string> linkRewriter, IEnumerable<string>? styleURLs)
{
if (config.IncludeBootstrap_CSS)
{
Expand All @@ -42,6 +42,14 @@ internal static void AddOptionalsToDoc(Configuration config, ICollection<(string
htmlDoc.AddToHeader("<script src=\"https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js\" integrity=\"sha384-kenU1KFdBIe4zVF0s0G1M5b4hcpxyD9F7jL+jjXkk+Q2h455rYXK/7HAuoJl+0I4\" crossorigin=\"anonymous\"></script>");
}

if (styleURLs != null)
{
foreach (string styleSheetURL in styleURLs)
{
htmlDoc.AddToHeader($"<link href=\"{styleSheetURL}\" rel=\"stylesheet\">");
}
}

if (itemsInNavBar != null)
{
Navbar navbar = new(linkRewriter(config.HomePage))
Expand Down
2 changes: 1 addition & 1 deletion MarkdownToSiteGenerator/HTML/Navbar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace MarkdownToSiteGenerator.HTML
internal class Navbar : HtmlSymbolWithChildren
{
protected override string TagCode => "nav";
public override string CSSClass => "navbar navbar-expand-lg navbar-light bg-light";
public override string CSSClass => "navbar navbar-light bg-light";
public string? SiteName { get; set; }
public string HomePage { get; set; }
public Navbar(string homePage)
Expand Down
1 change: 1 addition & 0 deletions MarkdownToSiteGenerator/IFileWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ public interface IFileWriter<TPathOut>
bool FileExists(TPathOut path);
void Delete(TPathOut path);
Task Write(StringBuilder content, TPathOut destination);
Task Write(string content, TPathOut destination);
Task WriteBinary(Stream s, TPathOut pathOut);
}
}
4 changes: 2 additions & 2 deletions MarkdownToSiteGenerator/MarkdownFileConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public MarkdownFileConverter(IPathMapper<TPathIn, TPathOut> pathMapper,
this.writer = writer;
}

public async Task ConvertAndWriteHTML(SymbolisedDocument doc, TPathIn sourceLocation, Func<string, string> rewriteLink, ICollection<(TPathIn sourceLocation, string title)>? inMenu, Configuration config)
public async Task ConvertAndWriteHTML(SymbolisedDocument doc, TPathIn sourceLocation, Func<string, string> rewriteLink, ICollection<(TPathIn sourceLocation, string title)>? inMenu, Configuration config, string[] styleURLs)
{
TPathOut destination = pathMapper.GetDestination(sourceLocation);
if (writer.FileExists(destination))
Expand All @@ -37,7 +37,7 @@ public async Task ConvertAndWriteHTML(SymbolisedDocument doc, TPathIn sourceLoca

var menu = inMenu?.Select(a=>(pathMapper.GetURLLocation(a.sourceLocation), a.title)).ToArray();
HTMLGenerator generator = new(doc, config);
StringBuilder sb = generator.Generate(rewriteLink, menu);
StringBuilder sb = generator.Generate(rewriteLink, menu, styleURLs);

await writer.Write(sb, destination);
}
Expand Down
38 changes: 29 additions & 9 deletions MarkdownToSiteGenerator/SiteGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ public SiteGenerator(ISourceFileProvider<TPathIn> sourceFileProvider, IPathMappe
public IEnumerable<TPathIn> GetInputFileLocations() => sourceFileProvider.GetFileLocations(FileTypes.All);
public IEnumerable<TPathOut> GetOutputFileLocations() => GetInputFileLocations().Select(pathMapper.GetDestination);
public IEnumerable<TPathOut> GetWillBeOverwritten() => GetOutputFileLocations().Where(fileWriter.FileExists);


TPathIn[] GetStyleFileLocations() => sourceFileProvider.GetFileLocations(FileTypes.Style);

/// <summary>
/// Parses all inputs that the source file provider identifies as source code
Expand All @@ -53,21 +54,40 @@ public async Task Generate()

TitleToURLLookup<TPathIn> urlLookup = CreateURLLookup(docTitles, config);

string[] styleUrls = GetStyleFileLocations().Select(pathMapper.GetURLLocation).ToArray();


foreach ((TPathIn location, SymbolisedDocument doc) in docs)
{
await converter.ConvertAndWriteHTML(doc, location, urlLookup.GetURL, docTitles, config);
await converter.ConvertAndWriteHTML(doc, location, urlLookup.GetURL, docTitles, config, styleUrls);
}

await GenerateSiteMapIfConfigAllows(config, docTitles, urlLookup.GetURL);
await GenerateSiteMapIfConfigAllows(config, docTitles, urlLookup.GetURL, styleUrls);

await CopyImageFiles();

foreach(var imInfo in sourceFileProvider.GetFileLocations(FileTypes.Images))
await CopyCSSFiles();
}

private async Task CopyImageFiles()
{
foreach (var imInfo in sourceFileProvider.GetFileLocations(FileTypes.Images))
{
using Stream s = sourceFileProvider.GetImageFileContent(imInfo);
await fileWriter.WriteBinary(s, pathMapper.GetDestination(imInfo));
}

}



private async Task CopyCSSFiles()
{
foreach (var cssInfo in GetStyleFileLocations())
{
string content = await sourceFileProvider.GetFileContent(cssInfo);
await fileWriter.Write(content, pathMapper.GetDestination(cssInfo));
}
}

private TitleToURLLookup<TPathIn> CreateURLLookup(List<(TPathIn location, string title)> docTitles, Configuration config)
{
Expand All @@ -83,7 +103,7 @@ private TitleToURLLookup<TPathIn> CreateURLLookup(List<(TPathIn location, string
return urlLookup;
}

private async Task GenerateSiteMapIfConfigAllows(Configuration config, IEnumerable<(TPathIn path, string title)> locations, Func<string, string> rewriteLink)
private async Task GenerateSiteMapIfConfigAllows(Configuration config, IEnumerable<(TPathIn path, string title)> locations, Func<string, string> rewriteLink, IEnumerable<string>? styleURLs)
{
if (config.CreateSiteMaps)
{
Expand All @@ -92,7 +112,7 @@ private async Task GenerateSiteMapIfConfigAllows(Configuration config, IEnumerab
await WriteSiteMap_XML(config.DestinationDomain, locations.Select(a=>a.path));
}

await WriteSiteMap_HTML(config, locations, rewriteLink);
await WriteSiteMap_HTML(config, locations, rewriteLink, styleURLs);
}
}

Expand All @@ -102,10 +122,10 @@ private async Task WriteSiteMap_XML(string domain, IEnumerable<TPathIn> location
await fileWriter.Write(map, pathMapper.GetDestination_Sitemap_XML());
}

private async Task WriteSiteMap_HTML(Configuration config, IEnumerable<(TPathIn path, string title)> locations, Func<string, string> rewriteLink)
private async Task WriteSiteMap_HTML(Configuration config, IEnumerable<(TPathIn path, string title)> locations, Func<string, string> rewriteLink, IEnumerable<string>? styleURLs)
{
HtmlDocument doc = SiteMapGenerator.CreateHTMLMap(locations.Select(a => ( new FilePath(pathMapper.GetURLLocation(a.path)), a.title)).ToList());
HTMLGenerator.AddOptionalsToDoc(config, null, doc, rewriteLink);
HTMLGenerator.AddOptionalsToDoc(config, null, doc, rewriteLink, styleURLs);
StringBuilder map = doc.Write(new StringBuilder());
await fileWriter.Write(map, pathMapper.GetDestination_Sitemap_HTML());
}
Expand Down
4 changes: 3 additions & 1 deletion MarkdownToSiteGeneratorUnitTests/FileSourceProviderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,15 @@ public async Task GetConfigurationFileContent_Empty()
[DataRow(FileTypes.All)]
[DataRow(FileTypes.SourceFiles)]
[DataRow(FileTypes.Images)]
[DataRow(FileTypes.Style)]
public void GetFileLocations_Empty(FileTypes types)
{
Assert.IsNotNull(fsp);
Assert.AreEqual(0, fsp.GetFileLocations(types).Length);
}

[TestMethod]
[DataRow(FileTypes.All, 0, 9)]
[DataRow(FileTypes.All, 0, 10)]
[DataRow(FileTypes.SourceFiles, 7, 2)]
[DataRow(FileTypes.Images, 0, 7)]
[DataRow(FileTypes.None, 0, 0)]
Expand All @@ -88,6 +89,7 @@ public void GetFileLocations(FileTypes ft, int expectedFrom, int count)
dir + "my-image-6.gif",
dir + "bob.md",
dir + "bob2.md",
dir + "my style.css",
dir + "config.ini", // shouldn't be found
dir + ".gitattributes", // shouldn't be found
dir + ".gitignore", // shouldn't be found
Expand Down
24 changes: 24 additions & 0 deletions MarkdownToSiteGeneratorUnitTests/HTML/HTMLGeneratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -169,5 +169,29 @@ public void MarkdownDocToHtmlDoc_ParagraphWithLink()
});
}


[TestMethod]
public void Generate_CSSIncluded()
{
string raw =@"Look at this [photograph](https://example.com/photograph)";

var doc = new MarkdownParser().Parse(raw);

HtmlSymbol made = HTMLGenerator.ToHTMLSymbols(doc, raw);

SymbolisedDocument d = new(raw);
HTMLGenerator gen = new(d, new Configuration());
string s = gen.Generate(a => a, styleURLs: new string[] { "/my-style.css" }).ToString();

string expected = "<link href=\"/my-style.css\" rel=\"stylesheet\">";

int index = s.IndexOf(expected);
Assert.AreNotEqual(-1, index);
int startOfHead = s.IndexOf("<head>");
Assert.IsTrue(index > startOfHead);
int endOfHead = s.IndexOf("</head>");
Assert.IsTrue(index < endOfHead);
}

}
}
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ MyTiTlE == mytitle
My_TiTlE == my_title
```

## Style

Bootstrap is automatically included, unless turned off in the config file. Additional CSS files should be placed in the root directory. Any that are found are linked to in all generated html files.

## Limitations

* This is not intended to be a complete solution for all use cases and only covers a subset of Markdown syntax. It is readily extensible, though.
Expand Down

0 comments on commit c04007f

Please sign in to comment.