public static LoadToc ( string tocContent, string filePath ) : TocViewModel | ||
tocContent | string | |
filePath | string | |
Результат | TocViewModel |
public static TocViewModel LoadToc(string tocContent, string filePath)
{
ParseState state = new InitialState(filePath);
var rules = new ParseRule[]
{
new TopicTocParseRule(),
new ExternalLinkTocParseRule(),
new TopicXrefAutoLinkTocParseRule(),
new TopicXrefShortcutTocParseRule(),
new ContainerParseRule(),
new CommentParseRule(),
new WhitespaceParseRule(),
};
var content = tocContent.Replace("\r\n", "\n").Replace("\r", "\n");
int lineNumber = 1;
while (content.Length > 0)
{
state = state.ApplyRules(rules, ref content, ref lineNumber);
}
return state.Root;
}
// TODO: refactor the return value to TocRootViewModel public static TocItemViewModel LoadSingleToc(string file) { if (string.IsNullOrEmpty(file)) { throw new ArgumentNullException(nameof(file)); } var fileType = Utility.GetTocFileType(file); try { if (fileType == TocFileType.Markdown) { return(new TocItemViewModel { Items = MarkdownTocReader.LoadToc(EnvironmentContext.FileAbstractLayer.ReadAllText(file), file) }); } else if (fileType == TocFileType.Yaml) { return(LoadYamlToc(file)); } } catch (Exception e) { var message = $"{file} is not a valid TOC File: {e.Message}"; Logger.LogError(message); throw new DocumentException(message, e); } throw new NotSupportedException($"{file} is not a valid TOC file, supported toc files could be \"{Constants.TableOfContents.MarkdownTocFileName}\" or \"{Constants.TableOfContents.YamlTocFileName}\"."); }