private async Task <DocumentContent> GetContent(IFileInfo fileInfo, IDocumentLanguage lang) { var stream = fileInfo.CreateReadStream(); var reader = new StreamReader(stream); if (await reader.ReadLineAsync() != "---") { throw new FormatException(fileInfo.Name + "文件格式错误,属性解释失败。"); } Dictionary <string, string> attribute = new Dictionary <string, string>(); while (!reader.EndOfStream) { var value = await reader.ReadLineAsync(); if (value == "---") { break; } var data = value.Split(':'); if (data.Length != 2) { throw new FormatException(fileInfo.Name + "文件格式错误,属性解释失败。"); } attribute.Add(data[0].Trim(), data[1].Trim()); } var title = await reader.ReadLineAsync(); if (title.StartsWith("#")) { var i = title.IndexOf(' '); title = title.Substring(i + 1); } var content = await reader.ReadToEndAsync(); return(new DocumentContent( title, attribute.ContainsKey("keywords") ? attribute["keywords"].Split(',').ToList() : new List <string>(), attribute.ContainsKey("authors") ? attribute["authors"].Split(',').Select(t => GetAuthor(t)).ToList() : new List <IDocumentAuthor>(), fileInfo.LastModified.DateTime, fileInfo.LastModified.DateTime, content, lang)); }
public IDocumentContent GetContent(string path, IDocumentLanguage lang, out IDocumentPage page) { if (!IsLoaded) { throw new InvalidOperationException("未加载内容。"); } if (lang == null) { throw new ArgumentNullException(nameof(lang)); } if (!string.IsNullOrEmpty(path)) { path = path.ToLower(); if (!_Pages.TryGetValue(path, out page)) { return(null); } } else { page = Provider.Pages.First(); } var content = page.GetContent(lang); if (content == null) { foreach (var prefer in PreferredLanguage) { content = page.GetContent(prefer); if (content != null) { break; } } } return(content); }
public IDocumentContent GetContent(IDocumentLanguage lang) { return(Contents.SingleOrDefault(t => t.Language == lang)); }
public DocumentContent(string title, IList <string> keywords, IList <IDocumentAuthor> authors, DateTime createDate, DateTime editDate, string content, IDocumentLanguage language) { if (keywords == null) { throw new ArgumentNullException(nameof(keywords)); } if (authors == null) { throw new ArgumentNullException(nameof(authors)); } Title = title ?? throw new ArgumentNullException(nameof(title)); Keywords = new ReadOnlyCollection <string>(keywords); Authors = new ReadOnlyCollection <IDocumentAuthor>(authors); CreateDate = createDate; EditDate = editDate; Content = content ?? throw new ArgumentNullException(nameof(content)); Language = language ?? throw new ArgumentNullException(nameof(language)); }
public async Task LoadAsync() { _Client = new HttpClient(); _Client.BaseAddress = new Uri("https://api.github.com"); var message = await _Client.GetAsync("/repos/" + Owner + "/" + Repository + "/contents/" + "topic.md"); if (message.StatusCode != System.Net.HttpStatusCode.OK) { throw new NotSupportedException("找不到topic.md文件。"); } var stream = await message.Content.ReadAsStreamAsync(); var reader = new StreamReader(stream); Title = await reader.ReadLineAsync(); if (!Title.StartsWith("# ")) { throw new FormatException("topic.md文件格式错误,标题解释失败。"); } List <IDocumentLanguage> languages = new List <IDocumentLanguage>(); while (true) { var value = await reader.ReadLineAsync(); if (string.IsNullOrEmpty(value)) { break; } var data = value.Split(':'); if (data.Length != 2) { throw new FormatException("topic.md文件格式错误,语言解释失败。"); } data[0] = data[0].Trim(); data[1] = data[1].Trim(); if (!data[0].StartsWith("[") || !data[0].EndsWith("]")) { throw new FormatException("topic.md文件格式错误,语言解释失败。"); } languages.Add(new DocumentLanguage(data[1], data[0].Substring(1, data[0].Length - 2))); } Languages = new ReadOnlyCollection <IDocumentLanguage>(languages); DefaultLanguage = languages[0]; int current = 0; int space, start, end; Stack <List <IDocumentPage> > childrenStack = new Stack <List <IDocumentPage> >(); var root = new List <IDocumentPage>(); Pages = new ReadOnlyCollection <IDocumentPage>(root); childrenStack.Push(root); while (!reader.EndOfStream) { var value = await reader.ReadLineAsync(); if (string.IsNullOrEmpty(value)) { continue; } if (!value.StartsWith("#")) { throw new FormatException("topic.md文件格式错误,目录解释失败。"); } space = value.IndexOf(' '); if (space == -1 || space > ++current) { throw new FormatException("topic.md文件格式错误,目录解释失败。"); } if (space != current) { var skip = current - space; for (int i = 0; i < skip; i++) { childrenStack.Pop(); current--; } } start = value.IndexOf('[', space + 1); if (start == -1) { throw new FormatException("topic.md文件格式错误,目录解释失败。"); } end = value.IndexOf(']', start + 1); if (end == -1) { throw new FormatException("topic.md文件格式错误,目录解释失败。"); } string name = value.Substring(start + 1, end - start - 1); start = value.IndexOf('(', end + 1); if (start == -1) { throw new FormatException("topic.md文件格式错误,目录解释失败。"); } end = value.IndexOf(')', start + 1); if (end == -1) { throw new FormatException("topic.md文件格式错误,目录解释失败。"); } string path = value.Substring(start + 1, end - start - 1); if (!path.EndsWith(".md")) { throw new FormatException("topic.md文件格式错误,目录解释失败。"); } List <IDocumentContent> contents = await GetContents(path); List <IDocumentPage> children = new List <IDocumentPage>(); DocumentPage page = new DocumentPage(name, contents, children); childrenStack.Peek().Add(page); childrenStack.Push(children); } message.Dispose(); }