public static void SelectUserAnswers(string site, string subdir, string target, int userid) { string datadir = "..\\..\\..\\..\\data\\" + site + "\\"; string postsdir = Path.Combine(datadir, subdir + "\\"); string targetdir = Path.Combine(datadir, target + "\\"); string path; if (!Directory.Exists(targetdir)) { Directory.CreateDirectory(targetdir); } Console.WriteLine("Copying answers of user {0} to {1}...", userid, targetdir); PostSet posts = PostSet.LoadFromDir(postsdir, site); Console.WriteLine("Answers: {0}", posts.AllMarkdownAnswers.Count); int c = 0; foreach (int a in posts.AllMarkdownAnswers.Keys) { AnswerMarkdown answer = posts.AllMarkdownAnswers[a]; if (answer.UserId.Trim() != userid.ToString().Trim()) { continue; } try { QuestionMarkdown question = answer.Parent; if (question != null) { if (String.IsNullOrEmpty(question.Title)) { question.Title = "Question " + answer.QuestionId.ToString(); } answer.Title = "Ответ на \"" + question.Title.ToString() + "\""; } path = Path.Combine(targetdir, "A" + a.ToString() + ".md"); using (TextWriter wr = new StreamWriter(path, false, Encoding.UTF8)) { answer.ToMarkdown(wr); } c++; } catch (Exception ex) { Console.WriteLine("Error on anser " + a.ToString()); Console.WriteLine(ex.GetType() + ": " + ex.Message); } } Console.WriteLine("Copied: {0}", c); }
public static void SaveQuestionsForSavedAnswers(string site, string subdir) { string datadir = "..\\..\\..\\..\\data\\" + site + "\\"; string postsdir = Path.Combine(datadir, subdir + "\\"); Console.WriteLine("Loading questions of existing answers ({0}, {1})...", site, subdir); PostSet posts = PostSet.LoadFromDir(postsdir, site); Dictionary <int, Question> questions = posts.Questions; Console.WriteLine("Answers without parent question: {0}", posts.MarkdownAnswers.Count); int n = 0; foreach (int a in posts.MarkdownAnswers.Keys) { try { SaveQuestion(site, posts.MarkdownAnswers[a].QuestionId); n++; if (n > 70) { break; } } catch (Exception ex) { Console.WriteLine(ex.GetType() + ": " + ex.Message); System.Threading.Thread.Sleep(20 * 1000); } } }
public static void RenderYamlTOC(string site, string title, PostSet posts, TextWriter wr) { string pagetitle = String.Format("{0}: {1}", site, title); wr.WriteLine("- name: \"{0}\"", pagetitle); wr.WriteLine(" href: index.md"); wr.WriteLine(" items:"); foreach (int key in posts.Questions.Keys) { string s = System.Net.WebUtility.HtmlDecode(posts.Questions[key].DataDynamic.title); s = s.Replace("\\", "\\\\"); s = s.Replace("\"", "\\\""); wr.WriteLine(" - name: \"{0}\"", s); wr.WriteLine(" href: {0}.md", key); } List <int> keys = posts.MarkdownQuestions.Keys.ToList(); keys.Sort(); for (int i = keys.Count - 1; i >= 0; i--) { int key = keys[i]; string s = System.Net.WebUtility.HtmlDecode(posts.MarkdownQuestions[key].Title); s = s.Replace("\\", "\\\\"); s = s.Replace("\"", "\\\""); wr.WriteLine(" - name: \"{0}\"", s); wr.WriteLine(" href: {0}.md", key); } foreach (int key in posts.SingleAnswers.Keys) { wr.WriteLine(" - name: \"Answer {0}\"", key); wr.WriteLine(" href: {0}.md", key); } keys = posts.MarkdownAnswers.Keys.ToList(); keys.Sort(); for (int i = keys.Count - 1; i >= 0; i--) { int key = keys[i]; string s = posts.MarkdownAnswers[key].Title; s = s.Replace("\\", "\\\\"); s = s.Replace("\"", "\\\""); wr.WriteLine(" - name: \"{0}\"", s); wr.WriteLine(" href: {0}.md", key); } }
public static void RenderTOC(string site, string title, PostSet posts, TextWriter wr) { string pagetitle = String.Format("{0}: {1}", site, title); RenderHeader(pagetitle, wr); if (posts.Questions.Count + posts.MarkdownQuestions.Count > 0) { wr.WriteLine("<h2>Questions</h2>"); } foreach (int key in posts.Questions.Keys) { wr.WriteLine("<p><a href=\"{0}.md\">{1}</a></p>", key, posts.Questions[key].DataDynamic.title); } List <int> keys = posts.MarkdownQuestions.Keys.ToList(); keys.Sort(); for (int i = keys.Count - 1; i >= 0; i--) { int key = keys[i]; wr.WriteLine("<p><a href=\"{0}.md\">{1}</a></p>", key, posts.MarkdownQuestions[key].Title); } if (posts.SingleAnswers.Count + posts.MarkdownAnswers.Count > 0) { wr.WriteLine("<h2>Answers</h2>"); } foreach (int key in posts.SingleAnswers.Keys) { wr.WriteLine("<p><a href=\"{0}.md\">Answer {1}</a></p>", key, key); } keys = posts.MarkdownAnswers.Keys.ToList(); keys.Sort(); for (int i = keys.Count - 1; i >= 0; i--) { int key = keys[i]; wr.WriteLine("<p><a href=\"{0}.md\">{1}</a></p>", key, posts.MarkdownAnswers[key].Title); } RenderBottom(wr); }
public static Dictionary <int, Question> LoadAllFromDir(string path, string site) { return(PostSet.LoadFromDir(path, site).Questions); }
public static void Generate(string site, string subdir, string toc_title) { string datadir = "..\\..\\..\\..\\data\\" + site + "\\"; string postsdir = Path.Combine(datadir, subdir + "\\"); string htmldir = "..\\..\\..\\..\\html\\" + site + "\\"; string targetdir = Path.Combine(htmldir, subdir + "\\"); string path; TextWriter wr; if (!Directory.Exists(htmldir)) { Directory.CreateDirectory(htmldir); } if (!Directory.Exists(targetdir)) { Directory.CreateDirectory(targetdir); } Console.WriteLine("Generating HTML files ({0}, {1})...", site, subdir); PostSet posts = PostSet.LoadFromDir(postsdir, site); Dictionary <int, Question> questions = posts.Questions; Console.WriteLine("JSON questions: {0}", questions.Count); foreach (int q in questions.Keys) { string title = questions[q].DataDynamic.title; path = Path.Combine(targetdir, q.ToString() + ".md"); wr = new StreamWriter(path, false); using (wr) { questions[q].GenerateHTML(wr); } } Console.WriteLine("JSON answers: {0}", posts.SingleAnswers.Count); foreach (int a in posts.SingleAnswers.Keys) { path = Path.Combine(targetdir, a.ToString() + ".md"); wr = new StreamWriter(path, false); using (wr) { HTML.RenderHeader(a, wr); posts.SingleAnswers[a].GenerateHTML(wr); HTML.RenderBottom(wr); } } Console.WriteLine("Markdown questions: {0}", posts.MarkdownQuestions.Count); foreach (int q in posts.MarkdownQuestions.Keys) { path = Path.Combine(targetdir, q.ToString() + ".md"); wr = new StreamWriter(path, false); using (wr) { posts.MarkdownQuestions[q].GenerateHTML(wr); } } Console.WriteLine("Markdown answers: {0}", posts.MarkdownAnswers.Count); foreach (int a in posts.MarkdownAnswers.Keys) { path = Path.Combine(targetdir, a.ToString() + ".md"); wr = new StreamWriter(path, false); using (wr) { HTML.RenderHeader(posts.MarkdownAnswers[a].Title, wr); posts.MarkdownAnswers[a].GenerateHTML(wr); HTML.RenderBottom(wr); } } Console.WriteLine("Generating TOC ({0}: {1})...", site, toc_title); path = Path.Combine(targetdir, "index.md"); wr = new StreamWriter(path, false); using (wr) { HTML.RenderTOC(site, toc_title, posts, wr); } Console.WriteLine("Generating toc.yml ({0}: {1})...", site, toc_title); path = Path.Combine(targetdir, "toc.yml"); wr = new StreamWriter(path, false); using (wr) { HTML.RenderYamlTOC(site, toc_title, posts, wr); } }