示例#1
0
        public static MDParagraph Merge(MDStruct mdStruct, params MDParagraph[] paragraphs)
        {
            if (paragraphs == null || paragraphs.Length == 0)
            {
                throw new ArgumentNullException("paragraphs", "Список объединяемых текстов не может быть пустым.");
            }

            if (paragraphs.Length == 1)
            {
                return(paragraphs[0]);
            }

            int    level  = paragraphs[0].Level;
            string header = paragraphs[0].Header;

            foreach (MDParagraph p in paragraphs)
            {
                if (p.Level != level)
                {
                    throw new ArgumentException("Все параграфы должны иметь одинаковый уровень.");
                }
                if (p.Header != header)
                {
                    throw new ArgumentException("Все параграфы должны иметь одинаковый заголовок.");
                }
            }

            MDParagraph merged = new MDParagraph(level, header);

            merged.Text = MergeParagraphsText(paragraphs, mdStruct == null ? MDMergeStrategy.Union : mdStruct.MergeStrategy);

            UnionSubparagraphs(merged.SubParagraphs, paragraphs, mdStruct == null ? null : mdStruct.Substruct);

            return(merged);
        }
示例#2
0
        public static void Main(string[] args)
        {
            MergeParams options;

            try
            {
                options = ParseArgs(args);
            }
            catch (ArgumentException argEx)
            {
                Console.WriteLine("Ошибка: ", argEx.Message);
                return;
            }

            Console.WriteLine("Read markdown files:");

            List <MDParagraph> markdowns = new List <MDParagraph>();

            foreach (string mdPath in options.InputPaths)
            {
                string fileName = Path.GetFileName(mdPath);

                Console.WriteLine("{0}: Read lines...", fileName);

                string[] mdLines = File.ReadAllLines(mdPath);

                Console.WriteLine("{0}: Parse markdown...", fileName);

                MDParagraph markdown = MDParser.ParseMarkdown(mdLines);
                markdowns.Add(markdown);
            }

            MDStruct mdStruct = null;

            if (!string.IsNullOrEmpty(options.StructPath))
            {
                Console.WriteLine("Load struct");

                using (Stream fMdStruct = File.Open(options.StructPath, FileMode.Open))
                {
                    XDocument mdStructXml = XDocument.Load(fMdStruct);
                    mdStruct = MDStruct.LoadFromXml(mdStructXml.Root);
                }
            }

            Console.WriteLine("Merge files...");

            MDParagraph merged = MDMerger.Merge(mdStruct, markdowns.ToArray());

            Console.WriteLine("Write merged Markdown to {0}", Path.GetFileName(options.ResultPath));

            File.WriteAllLines(options.ResultPath, merged.GetLines());
        }
示例#3
0
        public static MDParagraph ParseMarkdown(string[] text)
        {
            int iLine = 0;

            //skip empty lines
            SkipEmptyLines(text, ref iLine);

            //parse empty lines
            MDParagraph markdown = ParseParagraph(text, ref iLine, 1);

            return(markdown);
        }
示例#4
0
        public static MDParagraph ParseParagraph(string[] text, ref int iLine, int level)
        {
            if (iLine >= text.Count())
            {
                return(null);
            }

            MDParagraph paragraph = new MDParagraph(level);

            //parse header
            Match headerMatch = HeaderTmpl.Match(text[iLine]);

            if (!headerMatch.Success)
            {
                throw new FormatException(string.Format("Строка {0}: Ожидался заголовок.", iLine + 1));
            }
            string headerMark = headerMatch.Groups["Marker"].Value;

            if (headerMark.Length < level)
            {
                return(null);
            }
            if (headerMark.Length != level)
            {
                throw new FormatException(string.Format("Строка {0}. Ожидался заголовок уровня: {1}", iLine + 1, level));
            }
            paragraph.Header = headerMatch.Groups["HeaderText"].Value.Trim().TrimEnd('#').TrimEnd();
            iLine++;

            SkipEmptyLines(text, ref iLine);

            //add all non header lines to paragraph text
            paragraph.Text.AddRange(ReadParagraphText(text, ref iLine));

            //parse subparagraphs
            while (true)
            {
                MDParagraph p = ParseParagraph(text, ref iLine, level + 1);

                if (p == null)
                {
                    break;
                }

                paragraph.SubParagraphs.Add(p);
            }

            return(paragraph);
        }