/// <summary> /// Initializes this instance. /// </summary> public bool TryRunFrontMatter(IFrontMatter frontMatter, TemplateObject obj, ScriptObject newGlobal = null) { if (frontMatter == null) { throw new ArgumentNullException(nameof(frontMatter)); } if (obj == null) { throw new ArgumentNullException(nameof(obj)); } var context = CreatePageContext(); context.PushGlobal(obj); if (newGlobal != null) { context.PushGlobal(newGlobal); } try { context.EnableOutput = false; context.TemplateLoader = new TemplateLoaderFromIncludes(Site); Site.SetValue(PageVariables.Site, this, true); frontMatter.Evaluate(context); } catch (ScriptRuntimeException exception) { LogException(exception); return(false); } return(true); }
public ScriptInstance(bool hasErrors, string sourceFilePath, IFrontMatter frontMatter, ScriptPage template) { HasErrors = hasErrors; SourceFilePath = sourceFilePath; FrontMatter = frontMatter; Template = template; }
public ContentViewModel(IFrontMatter entity, IMarkupProcessorFactory markupProcessorFactory) { Date = entity.DateTime.HasValue ? entity.DateTime.Value : DateTimeOffset.MinValue; Title = entity.Title; Content = entity.Content; ContentType = entity.ContentType.ToString(); HtmlContent = markupProcessorFactory.CreateInstance(entity.ContentType).Process(Content); Permalinks = entity.Permalinks; Tags = entity.Tags; }
public static bool TryParseFrontMatter(string path, out IFrontMatter frontMatter) { var success = false; try { frontMatter = ParseFrontMatter(path); success = true; } catch { frontMatter = null; } return success; }
public static bool TryParseFrontMatter(string path, out IFrontMatter frontMatter) { var success = false; try { frontMatter = ParseFrontMatter(path); success = true; } catch { frontMatter = null; } return(success); }
/// <summary> /// Parses a script with the specified content and path. /// </summary> /// <param name="scriptContent">Content of the script.</param> /// <param name="scriptPath">The script path.</param> /// <param name="parsingMode">The parsing mode.</param> /// <returns> /// The parsed script or null of an error occured /// </returns> /// <exception cref="System.ArgumentNullException">if <paramref name="scriptContent" /> or <paramref name="scriptPath" /> is null</exception> /// <remarks> /// If there are any parsing errors, the errors will be logged to the <see cref="Log" /> and <see cref="HasErrors" /> will be set to <c>true</c>. /// </remarks> public ScriptInstance ParseScript(string scriptContent, UPath scriptPath, ScriptMode parsingMode) { if (scriptContent == null) { throw new ArgumentNullException(nameof(scriptContent)); } if (scriptPath == null) { throw new ArgumentNullException(nameof(scriptPath)); } IFrontMatter frontmatter = null; TextPosition startPosition = default; if (parsingMode == ScriptMode.FrontMatterAndContent && scriptContent.Length > 3) { var span = scriptContent.AsSpan(); foreach (var parser in FrontMatterParsers) { if (parser.CanHandle(span)) { frontmatter = parser.TryParse(scriptContent, (string)scriptPath, out startPosition); break; } } parsingMode = ScriptMode.Default; } // Load parse the template var template = Template.Parse(scriptContent, scriptPath.FullName, null, new LexerOptions() { StartPosition = startPosition, Mode = parsingMode }); // If we have any errors, log them and set the errors flag on this instance if (template.HasErrors) { LogScriptMessages(template.Messages); } return(new ScriptInstance(template.HasErrors, (string)scriptPath, frontmatter, template.Page)); }
/// <summary> /// Initializes this instance. /// </summary> public bool TryRunFrontMatter(IFrontMatter frontMatter, ContentObject obj) { if (frontMatter == null) { throw new ArgumentNullException(nameof(frontMatter)); } if (obj == null) { throw new ArgumentNullException(nameof(obj)); } var context = new TemplateContext(Builtins); context.PushGlobal(obj); try { context.EnableOutput = false; context.TemplateLoader = new TemplateLoaderFromIncludes(Site); Site.SetValue(PageVariables.Site, this, true); frontMatter.Evaluate(context); } catch (ScriptRuntimeException exception) { LogException(exception); return(false); } finally { context.PopGlobal(); // We don't keep the site variable after this initialization Site.Remove(PageVariables.Site); } return(true); }
public PostViewModel(IFrontMatter entity, IMarkupProcessorFactory markupProcessorFactory) : base(entity, markupProcessorFactory) { }