Example #1
0
        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);
        }
Example #2
0
        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 Dictionary <int, Question> LoadAllFromDir(string path, string site)
 {
     return(PostSet.LoadFromDir(path, site).Questions);
 }
Example #4
0
        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);
            }
        }