本文整理汇总了C#中Pretzel.Logic.Templating.Context.SiteContext类的典型用法代码示例。如果您正苦于以下问题:C# SiteContext类的具体用法?C# SiteContext怎么用?C# SiteContext使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
SiteContext类属于Pretzel.Logic.Templating.Context命名空间,在下文中一共展示了SiteContext类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Execute
public void Execute(string[] arguments)
{
Tracing.Info("bake - transforming content into a website");
Settings.Parse(arguments);
if (string.IsNullOrWhiteSpace(Path))
{
Path = Directory.GetCurrentDirectory();
}
if (string.IsNullOrWhiteSpace(Engine))
{
Engine = InferEngineFromDirectory(Path);
}
var engine = templateEngines[Engine];
if (engine != null)
{
var watch = new Stopwatch();
watch.Start();
var context = new SiteContext { Folder = Path };
engine.Initialize();
engine.Process(context);
watch.Stop();
Tracing.Info(string.Format("done - took {0}ms", watch.ElapsedMilliseconds));
}
else
{
Tracing.Info(String.Format("Cannot find engine for input: '{0}'", Engine));
}
}
示例2: FromPage
//public static PageContext FromDictionary(SiteContext siteContext, IDictionary<string, object> metadata, string outputPath, string defaultOutputPath)
//{
// var context = new PageContext(siteContext, TODO)
// {
// OutputPath =
// metadata.ContainsKey("permalink")
// ? Path.Combine(outputPath, metadata["permalink"].ToString().ToRelativeFile())
// : defaultOutputPath
// };
// if (metadata.ContainsKey("title"))
// {
// context.Title = metadata["title"].ToString();
// }
// context.Bag = metadata;
// return context;
//}
public static PageContext FromPage(SiteContext siteContext, Page page, string outputPath, string defaultOutputPath)
{
var context = new PageContext(siteContext, page);
if (page.Bag.ContainsKey("permalink"))
{
context.OutputPath = Path.Combine(outputPath, page.Url.ToRelativeFile());
}
else
{
context.OutputPath = defaultOutputPath;
page.Bag.Add("permalink", page.File);
}
if (context.OutputPath.EndsWith("\\"))
{
context.OutputPath = Path.Combine(context.OutputPath, "index.html");
}
page.OutputFile = context.OutputPath;
if (page.Bag.ContainsKey("title"))
{
context.Title = page.Bag["title"].ToString();
}
if (string.IsNullOrEmpty(context.Title))
context.Title = siteContext.Title;
context.Content = page.Content;
context.Bag = page.Bag;
context.Bag.Add("id", page.Id);
context.Bag.Add("url", page.Url);
return context;
}
示例3: BuildContext
public SiteContext BuildContext(string path, string destinationPath, bool includeDrafts)
{
try
{
var context = new SiteContext
{
SourceFolder = path,
OutputFolder = destinationPath,
Posts = new List<Page>(),
Pages = new List<Page>(),
Config = _config,
Time = DateTime.Now,
UseDrafts = includeDrafts
};
context.Posts = BuildPosts(_config, context).OrderByDescending(p => p.Date).ToList();
BuildTagsAndCategories(context);
context.Pages = BuildPages(_config, context).ToList();
if (BeforeProcessingTransforms != null)
{
foreach (var transform in BeforeProcessingTransforms)
{
transform.Transform(context);
}
}
return context;
}
finally
{
pageCache.Clear();
}
}
示例4: EvaluateLink_url_is_well_formatted
public void EvaluateLink_url_is_well_formatted(string filePath, string expectedUrl)
{
var siteContext = new SiteContext { OutputFolder = @"C:\TestSite\_site" };
var page = new Page { Filepath = filePath };
Assert.Equal(expectedUrl, LinkHelper.EvaluateLink(siteContext, page));
}
示例5: Should_Compile_Single_Less_File
public void Should_Compile_Single_Less_File()
{
var lessContent = @"@brand_color: #4D926F;
#header {
color: @brand_color;
}
h2 {
color: @brand_color;
}";
var lessOutput = @"#header{color:#4d926f}h2{color:#4d926f}";
var filepath = @"c:\css\style.less";
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
{
{ HtmlFilePath, new MockFileData(PageContent)},
{ filepath, new MockFileData(lessContent) }
});
var minifier = new LessTransform(fileSystem);
var context = new SiteContext { SourceFolder = @"C:\", OutputFolder = @"C:\_site" };
context.Pages.Add(new NonProcessedPage { OutputFile = HtmlFilePath, Content = PageContent });
context.Pages.Add(new NonProcessedPage { OutputFile = filepath, Content = lessContent, Filepath = filepath });
minifier.Transform(context);
var minifiedFile = fileSystem.File.ReadAllText(@"c:\css\style.css", Encoding.UTF8);
Assert.Equal(lessOutput, minifiedFile);
}
示例6: BuildContext
public SiteContext BuildContext(string path)
{
try
{
var config = new Dictionary<string, object>();
var configPath = Path.Combine(path, "_config.yml");
if (fileSystem.File.Exists(configPath))
config = (Dictionary<string, object>)fileSystem.File.ReadAllText(configPath).YamlHeader(true);
if (!config.ContainsKey("permalink"))
config.Add("permalink", "/:year/:month/:day/:title.html");
var context = new SiteContext
{
SourceFolder = path,
OutputFolder = Path.Combine(path, "_site"),
Posts = new List<Page>(),
Pages = new List<Page>(),
Config = config,
Time = DateTime.Now,
};
context.Posts = BuildPosts(config, context).OrderByDescending(p => p.Date).ToList();
BuildTagsAndCategories(context);
context.Pages = BuildPages(config, context).ToList();
return context;
}
finally
{
pageCache.Clear();
}
}
示例7: BuildContext
#pragma warning restore 0649
public SiteContext BuildContext(string path)
{
var config = new Dictionary<string, object>();
if (File.Exists(Path.Combine(path, "_config.yml")))
config = (Dictionary<string, object>)File.ReadAllText(Path.Combine(path, "_config.yml")).YamlHeader(true);
if (!config.ContainsKey("permalink"))
config.Add("permalink", "/:year/:month/:day/:title.html");
var context = new SiteContext
{
SourceFolder = path,
OutputFolder = Path.Combine(path, "_site"),
Posts = new List<Page>(),
Pages = new List<Page>(),
Config = config,
Time = DateTime.Now,
};
BuildPosts(config, context);
BuildPages(config, context);
return context;
}
示例8: BuildPages
private IEnumerable<Page> BuildPages(IConfiguration config, SiteContext context)
{
var files = from file in fileSystem.Directory.GetFiles(context.SourceFolder, "*.*", SearchOption.AllDirectories)
let relativePath = MapToOutputPath(context, file)
where CanBeIncluded(relativePath)
select file;
foreach (var file in files)
{
if (!ContainsYamlFrontMatter(file))
{
yield return new NonProcessedPage
{
File = file,
Filepath = Path.Combine(context.OutputFolder, MapToOutputPath(context, file))
};
}
else
{
var page = CreatePage(context, config, file, false);
if (page != null)
yield return page;
}
}
}
示例9: Process
public void Process(SiteContext siteContext, bool skipFileOnError = false)
{
// Default rendering engine
if (_lightweightMarkupEngine == null)
{
_lightweightMarkupEngine = new CommonMarkEngine();
}
Tracing.Logger.Write(string.Format("LightweightMarkupEngine: {0}", _lightweightMarkupEngine.GetType().Name), Tracing.Category.Debug);
Context = siteContext;
PreProcess();
for (int index = 0; index < siteContext.Posts.Count; index++)
{
var p = siteContext.Posts[index];
var previous = GetPrevious(siteContext.Posts, index);
var next = GetNext(siteContext.Posts, index);
ProcessFile(siteContext.OutputFolder, p, previous, next, skipFileOnError, p.Filepath);
}
for (int index = 0; index < siteContext.Pages.Count; index++)
{
var p = siteContext.Pages[index];
var previous = GetPrevious(siteContext.Pages, index);
var next = GetNext(siteContext.Pages, index);
ProcessFile(siteContext.OutputFolder, p, previous, next, skipFileOnError);
}
}
示例10: Transform
public void Transform(SiteContext siteContext)
{
var tipueContent = new TipueContent();
foreach (var post in siteContext.Posts)
{
var doc = new HtmlDocument();
doc.LoadHtml(post.Content);
tipueContent.Pages.Add(new TipuePage
{
Title = post.Title,
Tags = string.Join(" ", post.Tags),
Url = post.Url,
Text = doc.DocumentNode.InnerText
});
}
var settings = new JsonSerializerSettings
{
ContractResolver = new CamelCasePropertyNamesContractResolver()
};
var text = $"var tipuesearch = {JsonConvert.SerializeObject(tipueContent, settings)}";
File.WriteAllText(Path.Combine(siteContext.OutputFolder, @"assets\js\tipuesearch_content.js"), text);
}
示例11: BuildContext
#pragma warning restore 0649
public SiteContext BuildContext(string path)
{
if (!Path.IsPathRooted(path))
path = Path.Combine(Environment.CurrentDirectory, path);
var config = new Dictionary<string, object>();
if (File.Exists(Path.Combine(path, "_config.yml")))
config = (Dictionary<string, object>)File.ReadAllText(Path.Combine(path, "_config.yml")).YamlHeader(true);
if (!config.ContainsKey("permalink"))
config.Add("permalink", "/:year/:month/:day/:title.html");
var context = new SiteContext
{
SourceFolder = path,
OutputFolder = Path.Combine(path, "_site"),
Posts = new List<Page>(),
Pages = new List<Page>(),
};
BuildPosts(config, context);
foreach (var file in fileSystem.Directory.GetFiles(context.SourceFolder, "*.*", SearchOption.AllDirectories))
{
var relativePath = MapToOutputPath(context, file);
if (relativePath.StartsWith("_"))
continue;
if (relativePath.StartsWith("."))
continue;
var postFirstLine = SafeReadLine(file);
if (postFirstLine == null || !postFirstLine.StartsWith("---"))
{
context.Pages.Add(new NonProcessedPage
{
File = file,
Filepath = Path.Combine(context.OutputFolder, file)
});
continue;
}
var contents = SafeReadContents(file);
var header = contents.YamlHeader();
var page = new Page
{
Title = header.ContainsKey("title") ? header["title"].ToString() : "this is a post", // should this be the Site title?
Date = header.ContainsKey("date") ? DateTime.Parse(header["date"].ToString()) : DateTime.Now,
Content = Markdown.Transform(contents.ExcludeHeader()),
Filepath = GetPathWithTimestamp(context.OutputFolder, file),
File = file,
Bag = header,
};
context.Pages.Add(page);
}
return context;
}
示例12: Paginator
public Paginator(SiteContext site, int totalPages, int perPage, int page)
{
this.site = site;
TotalPosts = site.Posts.Count;
TotalPages = totalPages;
PerPage = perPage;
Page = page;
}
示例13: BuildPosts
private void BuildPosts(Dictionary<string, object> config, SiteContext context)
{
var postsFolder = Path.Combine(context.SourceFolder, "_posts");
if (fileSystem.Directory.Exists(postsFolder))
{
foreach (var file in fileSystem.Directory.GetFiles(postsFolder, "*.*", SearchOption.AllDirectories))
{
BuildPost(config, context, file);
}
context.Posts = context.Posts.OrderByDescending(p => p.Date).ToList();
}
}
示例14: Transform
public void Transform(SiteContext siteContext)
{
var shouldCompile = new List<Page>();
//Process to see if the site has a CSS file that doesn't exist, and should be created from LESS files.
//This is "smarter" than just compiling all Less files, which will crash if they're part of a larger Less project
//ie, one file pulls in imports, individually they don't know about each other but use variables
foreach (var file in siteContext.Pages.Where(p => p.OutputFile.EndsWith(".html") && fileSystem.File.Exists(p.OutputFile)))
{
var doc = new HtmlDocument();
var fileContents = fileSystem.File.ReadAllText(file.OutputFile);
doc.LoadHtml(fileContents);
var nodes = doc.DocumentNode.SelectNodes("/html/head/link[@rel='stylesheet']");
if (nodes != null)
foreach (HtmlNode link in nodes)
{
var cssfile = link.Attributes["href"].Value;
// If the file is not local, ignore it
var matchingIgnoreProtocol = ExternalProtocols.FirstOrDefault(cssfile.StartsWith);
if (matchingIgnoreProtocol != null)
continue;
//If the file exists, ignore it
if (fileSystem.File.Exists(Path.Combine(siteContext.OutputFolder, cssfile)))
continue;
//If there is a CSS file that matches the name, ignore, could be another issue
if (siteContext.Pages.Any(p => p.OutputFile.Contains(cssfile)))
continue;
var n = cssfile.Replace(".css", ".less");
n = n.Replace('/', '\\');
var cssPageToCompile = siteContext.Pages.FirstOrDefault(f => f.OutputFile.Contains(n));
if (cssPageToCompile != null && !shouldCompile.Contains(cssPageToCompile))
{
shouldCompile.Add(cssPageToCompile);
}
}
}
foreach (var less in shouldCompile)
{
filePath = less.OutputFile;
fileSystem.File.WriteAllText(less.OutputFile.Replace(".less", ".css"), ProcessCss(siteContext, less.Filepath));
fileSystem.File.Delete(less.OutputFile);
}
}
示例15: WriteRedirectFile
private void WriteRedirectFile(SiteContext siteContext, Page post, IEnumerable<string> sourceUrls) {
var targetUrl = post.Url;
var content = String.Format(Templates.Redirect, targetUrl);
foreach (var sourceUrl in sourceUrls) {
try {
var directory = _fileSystem.Path.Combine(siteContext.OutputFolder, sourceUrl.TrimStart('/').Replace('/', _fileSystem.Path.DirectorySeparatorChar));
if (!_fileSystem.Directory.Exists(directory)) {
_fileSystem.Directory.CreateDirectory(directory);
}
_fileSystem.File.WriteAllText(_fileSystem.Path.Combine(directory, "index.html"), content);
} catch (Exception ex) {
Console.WriteLine("Generating redirect for {0} at {1} failed:{2}{3}", post.Id, sourceUrl, Environment.NewLine, ex);
}
}
}