using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; namespace SoyText { public class ParsedDocument { Document src; public Dictionary values; public String cdr; public bool keyExists(String key) { return values.Keys.Contains(key); } public String getValue(String key) { return keyExists(key) ? values[key] : null; } public ParsedDocument(Document src) { this.src = src; values = new Dictionary(); init(); } public void setValue(String key, String value) { var resting = false; cdr = ""; var newLines = ""; foreach (var line in Regex.Split(src.content, "\r*\n")) { if (resting) { newLines += line + "\n"; continue; } var m = Regex.Match(line, "^[ \t]*$"); var valueSetted = false; if (m.Success) { if (!valueSetted) { newLines += key + ": " + value + "\n"; } resting = true; } var m2 = Regex.Match(line, "([^:]+): *(.*)"); if (m2.Success) { var k = m2.Groups[1].Value; var v = m2.Groups[2].Value; if (k.Equals(key)) { newLines += key + ": " + value + "\n"; valueSetted = true; } else { newLines += line + "\n"; } } else { newLines += line + "\n"; } } src.content = newLines; init(); } void init() { var resting = false; cdr = ""; foreach (var line in Regex.Split(src.content,"\r*\n")) { if (resting) { cdr += line + "\n"; continue; } var m=Regex.Match(line, "^[ \t]*$"); if (m.Success) { resting = true; } var m2 = Regex.Match(line, "([^:]+): *(.*)"); if (m2.Success) { values[m2.Groups[1].Value] = m2.Groups[2].Value; } } } } }