/* * Parsing begins with a call to GetFrontMatter function. It isolates the * front matter from the markdown string given as argument. We also need * the input file path for reporting possible errors. Nothing is read from * the file, though. * * ParseFrontMatter returns the value of the special parameter `template`. * This parameter controls which page template to use, and it needs to be * reseted to its default value when it is not present. It works * differently from other parameters which hold their value until a new * value is set. If the `template` parameter is not present or the whole * front matter is absent, null is returned. */ public string ParseFrontMatter(string frontMatter, string filePath) { string result = null; if (frontMatter != null) { /* * Now we can create a new YamlStream and load the front matter * using it. The parameters can be iterated through as simple * key-value pairs. */ try { var yamlStream = new YamlStream(); yamlStream.Load(new StringReader(frontMatter)); var mapping = (YamlMappingNode)yamlStream.Documents[0].RootNode; foreach (var entry in mapping.Children) { var key = entry.Key.ToString(); var value = entry.Value.ToString(); /* * If we encounter the special parameter `template`, we store * its value in the result. Special treatment is required also * for parameters starting with underscore `_`. Those can * contain markdown formatting and thus they are automatically * converted to HTML. */ if (key.ToLower() == "template") { result = value; } else { _params.Add(key, key.StartsWith("_") ? Markdown.ToHtml(value, _pipeline) : value); } } } /* * Parsing errors are caught, supplemented with information on * which input file the error occurred, and re-thrown to the * main program which eventually reports them to the user. */ catch (YamlDotNet.Core.SyntaxErrorException e) { throw new LiterateException( "Invalid syntax in the front matter. Make sure that the YAML data " + "is defined according to the specification.", filePath, "https://johtela.github.io/LiterateCS/FrontMatter.html", e); } } return(result); }