private string ProcessLineElements(string line) { // Break the line into parts, recognizing escape quotes ("") StringBuilder builder = new StringBuilder(); // Convert foo@bar external ref syntax to standardized InterWiki behavior // This must happen here because the actual interpretation of these is as a behavior line = externalWikiRef.Replace(line, new MatchEvaluator(externalWikiRefMatch)); ArrayList parameters = new ArrayList(); MatchCollection matches; // temp var used in several places in this method int index = 0; // temp var used in several places in this method #region GatherEscapedText matches = EscapedTextRegex.Matches(line); if (matches.Count > 0) { builder.Length = 0; index = 0; foreach (Match match in matches) { builder.Append(line.Substring(index, (match.Index - index))); // skip behaviors for now. We match them so that the Regex engine does the work // of dealing with @@""@@ and ""@@"" type things. Behaviors handled in the next block if (match.Groups[1].Success) { builder.Append(match.Value); index = match.Index + match.Length; continue; } if (match.Value.StartsWith(PreParam)) Parameterize(builder, parameters, match.Value); else Parameterize(builder, parameters, match.Value.Substring(2, match.Length - 2 - 2)); index = match.Index + match.Length; } builder.Append(line.Substring(index)); line = builder.ToString(); } #endregion #region TranslateBehaviors string escapedTwo = "\\@\\@"; matches = BehaviorRegex.Matches(line); if (matches.Count > 0) { builder.Length = 0; // reset index = 0; foreach (Match match in matches) { builder.Append(line.Substring(index, (match.Index - index))); if (!match.Groups[1].Success) { builder.Append(match.Value); index = match.Index + match.Length; continue; } // OK, we have one string expr = match.Value.Substring(2, match.Value.Length - (2 * 2)); // Turn any escape sequences into unescaped @@ expr = expr.Replace(escapedTwo, BehaviorDelimiter); TopicContext tc = new TopicContext(Federation, NamespaceManager, new TopicVersionInfo(Federation, CurrentTopic)); BehaviorInterpreter interpreter = new BehaviorInterpreter(CurrentTopic == null ? "" : CurrentTopic.DottedName, expr, Federation, Federation.WikiTalkVersion, this); string replacement = null; if (!interpreter.Parse()) { // parse failed replacement = ErrorMessage(null, interpreter.ErrorString); } else { // parse succeeded if (!interpreter.EvaluateToPresentation(tc, _externalWikiMap)) { // eval failed replacement = ErrorMessage(null, interpreter.ErrorString); } else { // eval succeeded WikiOutput nOut = WikiOutput.ForFormat(Output.Format, Output); interpreter.Value.OutputTo(nOut); replacement = nOut.ToString(); } } Parameterize(builder, parameters, replacement); index = match.Index + match.Length; } if (index < line.Length) builder.Append(line.Substring(index)); line = builder.ToString(); } #endregion line = LinkHyperLinks(line); matches = Regex.Matches(line, urlPattern); // hyperlinks are exempt from any emoticons or textile or anything else. if (matches.Count > 0) { builder.Length = 0; index = 0; foreach (Match m in matches) { builder.Append(line.Substring(index, (m.Index - index))); Parameterize(builder, parameters, line.Substring(m.Index, m.Length)); index = m.Index + m.Length; } builder.Append(line.Substring(index)); line = builder.ToString(); } if (((bool)Federation.Application["DisableWikiEmoticons"] == false) && (NamespaceManager.DisableNamespaceEmoticons == false)) { line = ConvertEmoticons(line); // needs to come before textile because of precedence in overlappign formatting rules } line = SentencePairedDelimiters(line); line = TextileFormat(line, parameters); line = ColorsEtcFormat(line); matches = Regex.Matches(line, @"(?:\<nowiki\>)(\<a.*?\<\/a\>)"); if (matches.Count > 0) { builder.Length = 0; index = 0; foreach (Match m in matches) { Group g = m.Groups[1]; builder.Append(line.Substring(index, (m.Index - index))); Parameterize(builder, parameters, line.Substring(g.Index, g.Length)); index = m.Index + m.Length; } builder.Append(line.Substring(index)); line = builder.ToString(); } line = LinkWikiNames(line); line = ProcessWikiLinks(line); line = ReplaceParameters(line, parameters); return line; }
/// <summary> /// Break the supplied line into BeL sequences and non-BeL sequences. Run the BeL interpreter to process /// the BeL sequences and do regular wiki processing on the non-BeL sequences /// </summary> /// <param name="line"></param> /// <returns></returns> string TranslateBehaviorsAndEverythingElse(string input) { // Convert foo@bar external ref syntax to standardized InterWiki behavior // This must happen here because the actual interpretation of these is as a behavior string str = externalWikiRef.Replace(input, new MatchEvaluator(externalWikiRefMatch)); string escapedTwo = "\\@\\@"; // If no behaviors, just process normally and return if (str.IndexOf(BehaviorDelimiter) == -1) return TranslateLineElements(input); while (true) { // Find the next BehaviorDelimiter marker int start = str.IndexOf(BehaviorDelimiter); if (start == -1) break; // Find the end int end = str.IndexOf(BehaviorDelimiter, start + 2); if (end == -1) break; // OK, we have one string expr = str.Substring(start + 2, end - start - 2); // Turn any escape sequences into unescaped @@ expr = expr.Replace(escapedTwo,BehaviorDelimiter); TopicContext tc = new TopicContext(TheFederation, ContentBase, new TopicInfo(TheFederation, CurrentTopic)); BehaviorInterpreter interpreter = new BehaviorInterpreter(expr, TheFederation, TheFederation.WikiTalkVersion, this); string replacement = null; if (!interpreter.Parse()) { // parse failed replacement = ErrorMessage(null, interpreter.ErrorString); } else { // parse succeeded if (!interpreter.EvaluateToPresentation(tc, _ExternalWikiMap)) { // eval failed replacement = ErrorMessage(null, interpreter.ErrorString); } else { // eval succeeded WikiOutput nOut = WikiOutput.ForFormat(Output.Format, Output); interpreter.Value.OutputTo(nOut); replacement = nOut.ToString(); } foreach (CacheRule r in interpreter.CacheRules) AddCacheRule(r); } str = TranslateLineElements(str.Substring(0, start)) + replacement + TranslateLineElements(str.Substring(end + 2)); } return str; }
private void button3_Click(object sender, System.EventArgs e) { Federation fed = new Federation(textBoxFederationPath.Text, OutputFormat.Testing, null); BehaviorInterpreter interpreter = new BehaviorInterpreter("Input Form", textBoxInput.Text, CurrentFederation, 1, this); ClearCacheView(); if (Parse(interpreter) == null) { Fail("Unable to run; parse failed:" + interpreter.ErrorString); tabControl.SelectedTab = tabParser; } // Begin execution tabControl.SelectedTab = tabOutput; ExecutionContext ctx = new ExecutionContext(CurrentTopicContext); string final = null; if (!interpreter.EvaluateToPresentation(CurrentTopicContext, new Hashtable())) { Fail(interpreter.ErrorString); return; } WikiOutput output = WikiOutput.ForFormat(OutputFormat.Testing, null); interpreter.Value.ToPresentation(this).OutputTo(output); final = output.ToString(); Success(final); UpdateCacheView(interpreter.CacheRules); }
private string wikiBehaviorMatch(Match match) { string replacement = match.ToString(); string linestart = match.Groups["LineStart"].Value; string lineend = match.Groups["LineEnd"].Value; string expr = match.Groups["Behavior"].Value; match = null; bool doBehavior = false; if (insideTable.IsMatch(linestart)) { doBehavior = true; } else if (!preBehavior.IsMatch(linestart)) { doBehavior = true; } if (doBehavior) { TopicContext tc = new TopicContext(_fed, _mgr, new TopicVersionInfo(_fed, _behaviorTopic)); BehaviorInterpreter interpreter = new BehaviorInterpreter(_behaviorTopic == null ? "" : _behaviorTopic.DottedName, expr, _fed, _fed.WikiTalkVersion, this); if (!interpreter.Parse()) { //parse failed replacement = ErrorMessage(null, interpreter.ErrorString); } else { //parse succeeded if (!interpreter.EvaluateToPresentation(tc, _mgr.ExternalReferences)) { //eval failed replacement = ErrorMessage(null, interpreter.ErrorString); _processBehaviorToText = false; } else { //eval succeeded WikiOutput nOut = new WomDocument(null); interpreter.Value.OutputTo(nOut); replacement = nOut.ToString(); } } if (replacement.Contains("<script")) { replacement = Regex.Replace(replacement, @"<script(.*?)>", @"<script$1>", RegexOptions.Singleline); replacement = Regex.Replace(replacement, @"</script>", @"</script>"); } replacement = Regex.Replace(replacement, @"^\|\|", "||}", RegexOptions.Multiline); replacement = Regex.Replace(replacement, @" \|\|$| \|\|\r\n| \{\|\|\r\n|\|\|\r\n", " {||\r\n", RegexOptions.Multiline); replacement = Regex.Replace(replacement, @"(?<!\{|"""")\|\|(?!\}|"""")", @" {||}"); if (_processBehaviorToText) { replacement = replacement.Replace(" ", "\t"); replacement = escAmpersand.Replace(replacement, "&"); //full escape causes some pages to fail replacement = ProcessText(replacement, _behaviorTopic, _mgr, true, 350).ParsedDocument; } } replacement = linestart + replacement + lineend; return replacement; }