private static void ExtractStepInfo(Guide aGuide, Step aStep, string aFileContents, string aStepDirectory) { aStep.IsValid = true; bool validTitleFound = false; bool validStepNumberFound = false; bool wasNewLine = true; for (int index = 0; index < aFileContents.Length; index++) { var character = aFileContents[index]; if (wasNewLine && character == '#') { continue; } wasNewLine = character == '\r' || character == '\n'; if (TagParser.ParseTag(aFileContents, ref index, out var tag, out var tagContent, out var attributes, isIncrementIndex: true)) { if (SimpleTags.IsTag(SimpleTags.Tag.Title, tag)) { aStep.Title = SimpleTags.GetContent <string>(tagContent); validTitleFound = !string.IsNullOrEmpty(aStep.Title); } else if (SimpleTags.IsTag(SimpleTags.Tag.Image, tag)) { ClassEnum classEnum = ClassEnum.All; if (attributes.ContainsKey("class")) //TODO make hard reference { classEnum = SimpleTags.GetContent <ClassEnum>(attributes["class"]); } aStep.ImageSources[classEnum] = Path.Combine(aStepDirectory, SimpleTags.GetContent <string>(tagContent)); } else if (SimpleTags.IsTag(SimpleTags.Tag.StepNumber, tag)) { aStep.StepNumber = SimpleTags.GetContent <int>(tagContent); validStepNumberFound = true; } else if (SimpleTags.IsTag(SimpleTags.Tag.SubStep, tag)) { SubStep subStep = SubStep.Parse(aGuide, aStep, attributes, tagContent); if (subStep.IsValid) { aStep.SubSteps.Add(subStep); } } } } aStep.ValidateImageSources(); if (!validTitleFound || !validStepNumberFound) { aStep.IsValid = false; } }
public static Guide Parse(string aDirectory) { Guide result = new Guide(); bool titleFound = false; bool classFound = false; if (!File.Exists(Path.Combine(aDirectory, GuideInfoFile))) { return(result); } string guideFileContent = File.ReadAllText(Path.Combine(aDirectory, GuideInfoFile)); foreach (var line in guideFileContent.Split(Environment.NewLine.ToCharArray()).Select(l => l.Trim()).Where(l => !string.IsNullOrEmpty(l))) { int index = 0; if (TagParser.ParseTag(line, ref index, out string tag, out string tagContent, out var attributes, isIncrementIndex: false)) { if (SimpleTags.IsTag(SimpleTags.Tag.Title, tag)) { result.Title = SimpleTags.GetContent <string>(tagContent); titleFound = true; } else if (SimpleTags.IsTag(SimpleTags.Tag.Class, tag)) { result.Classes = SimpleTags.GetContent <ClassEnum>(tagContent); classFound = true; } } } result.IsValid = titleFound && classFound; if (result.IsValid) { ParseSteps(result, aDirectory); } return(result); }
private void AddText(string aText, ref bool isQuestChanges, Span aCurrentSpan = null, string aCurrentText = null, bool isUnderline = false) { var textLines = aText.Split(Environment.NewLine.ToCharArray()); if (aCurrentSpan == null) { aCurrentSpan = new Span(); _paragraph.Inlines.Add(aCurrentSpan); } if (aCurrentText == null) { aCurrentText = string.Empty; } string[] realLines = textLines.Where(t => !t.Trim().StartsWith("#") && !string.IsNullOrEmpty(t.Trim())).ToArray(); for (int i = 0; i < realLines.Length; i++) { string line = realLines[i]; for (int charInde = 0; charInde < line.Length; charInde++) { if (line[charInde] == '\t') { aCurrentText += " "; continue; } if (TagParser.ParseTag(line, ref charInde, out var tag, out var tagContent, out var attributes)) { //Regular text if (!string.IsNullOrEmpty(aCurrentText)) { aCurrentSpan.Inlines.Add(GetText(aCurrentText, isUnderline)); aCurrentText = string.Empty; } if (SimpleTags.IsTag(SimpleTags.Tag.Legend, tag)) { var legendColor = SimpleTags.GetContent <Color>(tagContent); aCurrentSpan.Inlines.Add(CreateLegendRectangle(legendColor)); } else if (SimpleTags.IsTag(SimpleTags.Tag.TLoc, tag)) { var tloc = SimpleTags.GetContent <Point>(tagContent); if (double.IsNaN(tloc.X) || double.IsNaN(tloc.Y)) { aCurrentSpan.Inlines.Add(GetText("(error)", isUnderline)); } else { var tlocRun = new TLocInline(this, tloc, tagContent); aCurrentSpan.Inlines.Add(tlocRun); } } else if (SimpleTags.IsTag(SimpleTags.Tag.Bold, tag)) { var boldSpan = new Span(); boldSpan.FontWeight = FontWeights.Bold; aCurrentSpan.Inlines.Add(boldSpan); AddText(tagContent, ref isQuestChanges, aCurrentSpan: boldSpan, aCurrentText: aCurrentText, isUnderline: isUnderline); } else if (SimpleTags.IsTag(SimpleTags.Tag.Place, tag)) //Maybe do something else for this, same as bold atm { var boldSpan = new Span(); boldSpan.FontWeight = FontWeights.Bold; aCurrentSpan.Inlines.Add(boldSpan); AddText(tagContent, ref isQuestChanges, aCurrentSpan: boldSpan, aCurrentText: aCurrentText, isUnderline: isUnderline); } else if (SimpleTags.IsTag(SimpleTags.Tag.Italic, tag)) { var italicSpan = new Span(); italicSpan.FontStyle = FontStyles.Italic; aCurrentSpan.Inlines.Add(italicSpan); AddText(tagContent, ref isQuestChanges, aCurrentSpan: italicSpan, aCurrentText: aCurrentText, isUnderline: isUnderline); } else if (SimpleTags.IsTag(SimpleTags.Tag.Underline, tag)) { AddText(tagContent, ref isQuestChanges, aCurrentSpan: aCurrentSpan, aCurrentText: aCurrentText, isUnderline: true); } else if (SimpleTags.IsTag(SimpleTags.Tag.DONTFORGET, tag)) { var empesizedSpan = new Span(); empesizedSpan.FontStyle = FontStyles.Italic; empesizedSpan.FontWeight = FontWeights.ExtraBold; empesizedSpan.FontSize = aCurrentSpan.FontSize + 5; aCurrentSpan.Inlines.Add(empesizedSpan); AddText(tagContent, ref isQuestChanges, aCurrentSpan: empesizedSpan, aCurrentText: aCurrentText, isUnderline: isUnderline); } else if (SimpleTags.IsTag(SimpleTags.Tag.Picture, tag)) { string url = string.Empty; attributes.TryGetValue("url", out url); { //TODO make hard reference url = System.IO.Path.Combine(_stepDirectory, url); aCurrentSpan.Inlines.Add(new PictureLinkInline(this, url, tagContent)); } } else if (SimpleTags.IsTag(SimpleTags.Tag.LineBreak, tag)) { //aCurrentSpan.Inlines.Add(new LineBreak()); } else { var tagType = SimpleTags.GetTagFromString(tag); var colorSpan = new Span(); colorSpan.Foreground = new SolidColorBrush(TextColors.GetColorFromTag(tagType)); if (IsQuestTag(tagType)) { isQuestChanges = true; tagContent = HandleQuestTags(tagType, tagContent, attributes); } aCurrentSpan.Inlines.Add(colorSpan); if (attributes.Any(a => a.Key == "isTargetLink".ToLower() && bool.TryParse(a.Value, out bool isTargetLink) && isTargetLink)) { colorSpan.Inlines.Add(new CopyToClipboardInline(this, tagContent, $"/targetexact {tagContent}")); }