public static MDStruct LoadFromXml(XElement xml) { MDStruct mdStruct = new MDStruct(); XAttribute xHeader = xml.Attribute("header"); if (xHeader == null || string.IsNullOrWhiteSpace(xHeader.Value)) { throw new FormatException("Шаблон заголовка не может быть пустым (элемент: //mdstruct[header])"); } mdStruct.HeaderPattern = xHeader.Value; XAttribute xStrategy = xml.Attribute("strategy"); if (xStrategy != null && !string.IsNullOrWhiteSpace(xStrategy.Value)) { mdStruct.MergeStrategy = (MDMergeStrategy)Enum.Parse(typeof(MDMergeStrategy), (string)xStrategy.Value, true); } foreach (XElement child in xml.Elements("paragraph")) { mdStruct.Substruct.Add(LoadFromXml(child)); } return(mdStruct); }
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); }
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()); }