Describes a valid set of string tokens that define the format of a Nugget.
The standard numgget format is as follows: [[[Enter between %0 and %1 characters|||{1}|||{2}/// The %0 identifies refers to min number and the %1 refers to the max number. ]]] where: BeginToken = "[[[" EndToken = "]]]" DelimiterToken = "|||" CommentToken = "///"
Exemplo n.º 1
0
        public RecursiveNuggetParser(
            NuggetTokens nuggetTokens,
            NuggetParser.Context context)
        {
            m_nuggetTokens = nuggetTokens;
            m_context      = context;

            m_delimiterWithParameterBeginToken = nuggetTokens.DelimiterToken + nuggetTokens.ParameterBeginToken;
            m_parameterEndBeforeDelimiterToken = nuggetTokens.ParameterEndToken + nuggetTokens.DelimiterToken;
            m_parameterEndBeforeEndToken       = nuggetTokens.ParameterEndToken + nuggetTokens.EndToken;

            // Prep the regexes. We escape each token char to ensure it is not misinterpreted.
            // · Breakdown e.g. "\[\[\[(.+?)(?:\|\|\|(.+?))*(?:\/\/\/(.+?))?\]\]\]"
            m_tokensRegex = new Regex(
                string.Format(@"(?:{1}{4})|(?:{5}{1})|(?:{5}{3})|(?:{0})|(?:{1})|(?:{2})|(?:{3})",
                              EscapeString(m_nuggetTokens.BeginToken),
                              EscapeString(m_nuggetTokens.DelimiterToken),
                              EscapeString(m_nuggetTokens.CommentToken),
                              EscapeString(m_nuggetTokens.EndToken),
                              EscapeString(m_nuggetTokens.ParameterBeginToken),
                              EscapeString(m_nuggetTokens.ParameterEndToken)),
                RegexOptions.CultureInvariant
                | RegexOptions.Singleline);
            // RegexOptions.Singleline in fact enable multi-line nuggets.
        }
Exemplo n.º 2
0
 private void ParseAndComp(string nuggetString, Nugget rhs, bool equal = true)
 {
    // Arrange.
     NuggetTokens nuggetTokens = new NuggetTokens("[[[", "]]]", "|||", "///");
     NuggetParser nuggetParser = new NuggetParser(nuggetTokens, NuggetParser.Context.SourceProcessing);
    // Act.
     Nugget nugget = nuggetParser.BreakdownNugget(nuggetString);
    // Assert.
     if (equal) {
         Assert.AreEqual(nugget, rhs); }
     else {
         Assert.AreNotEqual(nugget, rhs); }
 }
Exemplo n.º 3
0
 public void NuggetParser_CanParseEntity01()
 {
    // Arrange.
     NuggetTokens nuggetTokens = new NuggetTokens("[[[", "]]]", "|||", "///");
     NuggetParser nuggetParser = new NuggetParser(nuggetTokens, NuggetParser.Context.SourceProcessing);
     string entity = "<p>[[[hello]]]</p><p>[[[there]]]</p>";
    // Act.
     nuggetParser.ParseString(entity, delegate(string nuggetString, int pos, Nugget nugget1, string entity1)
     {
         if (pos == 3) {
             Assert.AreEqual(nugget1, new Nugget { MsgId = "hello" }); }
         else if (pos == 21) {
             Assert.AreEqual(nugget1, new Nugget { MsgId = "there" }); }
         else {
             Assert.Fail(); }
         return null;
     });
 }
Exemplo n.º 4
0
        // Con

        public NuggetParser(
            NuggetTokens nuggetTokens,
            Context context)
        {
            m_nuggetTokens = nuggetTokens;
            m_context      = context;
            // Prep the regexes. We escape each token char to ensure it is not misinterpreted.
            // · Breakdown e.g. "\[\[\[(.+?)(?:\|\|\|(.+?))*(?:\/\/\/(.+?))?\]\]\]"
            m_regexNuggetBreakdown = new Regex(
                string.Format(@"{0}(.*?)(?:{1}(.{4}?))*(?:{2}(.+?))?{3}",
                              EscapeString(m_nuggetTokens.BeginToken),
                              EscapeString(m_nuggetTokens.DelimiterToken),
                              EscapeString(m_nuggetTokens.CommentToken),
                              EscapeString(m_nuggetTokens.EndToken),
                              m_context == Context.SourceProcessing ? "+" : "*"),
                RegexOptions.CultureInvariant
                | RegexOptions.Singleline);
            // RegexOptions.Singleline in fact enable multi-line nuggets.
        }
Exemplo n.º 5
0
        public void NuggetParser_ResponseMode_CanParseEntity_TwoParams_FirstNonEmpty_SecondEmpty() {
            var nuggetTokens = new NuggetTokens("[[[", "]]]", "|||", "///");
            NuggetParser nuggetParser = new NuggetParser(nuggetTokens, NuggetParser.Context.ResponseProcessing);
            var input = "[[[Title: %0, %1|||X|||]]]";
            var result = nuggetParser.ParseString(input, (nuggetString, pos, nugget, i_entity) => {
                Assert.IsTrue(nugget.IsFormatted);
                return nugget.MsgId;
            });

            Assert.AreEqual("Title: %0, %1", result);
        }
Exemplo n.º 6
0
        public void NuggetParser_SourceMode_CanParseEntity_EmptyParam() {
            var nuggetTokens = new NuggetTokens("[[[", "]]]", "|||", "///");
            NuggetParser nuggetParser = new NuggetParser(nuggetTokens, NuggetParser.Context.SourceProcessing);
            var input = "[[[Title: %0|||]]]";
            var result = nuggetParser.ParseString(input, (nuggetString, pos, nugget, i_entity) => {
                Assert.IsFalse(nugget.IsFormatted);
                return nugget.MsgId;
            });

            Assert.AreEqual("Title: %0|||", result);
        }
Exemplo n.º 7
0
        public void NuggetParser_ResponseMode_CanParseEntity_EmptyParam() {
            var nuggetTokens = new NuggetTokens("[[[", "]]]", "|||", "///");
            NuggetParser nuggetParser = new NuggetParser(nuggetTokens, NuggetParser.Context.ResponseProcessing);
            var input = "[[[Title: %0|||]]]";
            var result = nuggetParser.ParseString(input, (nuggetString, pos, nugget, i_entity) => {
                Assert.IsTrue(nugget.IsFormatted);
                var message = NuggetLocalizer.ConvertIdentifiersInMsgId(nugget.MsgId);
                message = String.Format(message, nugget.FormatItems);
                return message;
            });

            Assert.AreEqual("Title: ", result);
        }
Exemplo n.º 8
0
 public void NuggetParser_CanParseEntity_MultiLineNugget01()
 {
    // Arrange.
     NuggetTokens nuggetTokens = new NuggetTokens("[[[", "]]]", "|||", "///");
     NuggetParser nuggetParser = new NuggetParser(nuggetTokens, NuggetParser.Context.SourceProcessing);
     string entity = "<p>[[[hello\r\n%0|||{0}]]]</p><p>[[[there]]]</p>";
    // Act.
     int i = 0;
     nuggetParser.ParseString(entity, delegate(string nuggetString, int pos, Nugget nugget1, string entity1)
     {
         switch (i++) {
             case 0: {
                 Assert.AreEqual(nugget1, new Nugget { MsgId = "hello\r\n%0", FormatItems = new string[] { "{0}" } });
                 break;
             }
             case 1: {
                 Assert.AreEqual(nugget1, new Nugget { MsgId = "there" });
                 break;
             }
             default:
                 Assert.Fail();
                 break;
         }
         return null;
     });
 }
Exemplo n.º 9
0
 public void NuggetParser_CanParseEntity_CustomNuggetTokens03()
 {
    // Arrange.
     NuggetTokens nuggetTokens = new NuggetTokens("```", "'''", "###", "@@@");
     NuggetParser nuggetParser = new NuggetParser(nuggetTokens, NuggetParser.Context.SourceProcessing);
     string entity = "<p>```hello###{0}'''</p><p>```there###{0}###{1}@@@comment comment comment'''</p>";
    // Act.
     CanParseEntity_CustomNuggetTokens_Act(entity, nuggetParser);
 }
Exemplo n.º 10
0
 public void NuggetParser_CanParseEntity_CustomNuggetTokens02()
 {
    // Arrange.
     NuggetTokens nuggetTokens = new NuggetTokens("[[[:", ":]]]", "|||", "///");
     NuggetParser nuggetParser = new NuggetParser(nuggetTokens, NuggetParser.Context.SourceProcessing);
     string entity = "<p>[[[:hello|||{0}:]]]</p><p>[[[:there|||{0}|||{1}///comment comment comment:]]]</p>";
    // Act.
     CanParseEntity_CustomNuggetTokens_Act(entity, nuggetParser);
 }
Exemplo n.º 11
0
 public void NuggetParser_CanParseEntity03()
 {
    // Arrange.
     NuggetTokens nuggetTokens = new NuggetTokens("[[[", "]]]", "|||", "///");
     NuggetParser nuggetParser = new NuggetParser(nuggetTokens, NuggetParser.Context.SourceProcessing);
     string entity = "<p>[[[hello|||{0}]]]</p><p>[[[there|||{0}|||{1}///comment comment comment]]]</p>";
    // Act.
     nuggetParser.ParseString(entity, delegate(string nuggetString, int pos, Nugget nugget1, string entity1)
     {
         if (pos == 3) {
             Assert.AreEqual(nugget1, new Nugget { MsgId = "hello", FormatItems = new string[] { "{0}" } }); }
         else if (pos == 27) {
             Assert.AreEqual(nugget1, new Nugget { MsgId = "there", FormatItems = new string[] { "{0}", "{1}" }, Comment = "comment comment comment" }); }
         else {
             Assert.Fail(); }
         return null;
     });
 }
Exemplo n.º 12
0
    // Con

        public NuggetParser(
		    NuggetTokens nuggetTokens,
            Context context)
        {
            m_nuggetTokens = nuggetTokens;
            m_context = context;
           // Prep the regexes. We escape each token char to ensure it is not misinterpreted.
           // · Breakdown e.g. "\[\[\[(.+?)(?:\|\|\|(.+?))*(?:\/\/\/(.+?))?\]\]\]"
            m_regexNuggetBreakdown = new Regex(
                string.Format(@"{0}(.+?)(?:{1}(.{4}?))*(?:{2}(.+?))?{3}",
                    EscapeString(m_nuggetTokens.BeginToken), 
                    EscapeString(m_nuggetTokens.DelimiterToken), 
                    EscapeString(m_nuggetTokens.CommentToken), 
                    EscapeString(m_nuggetTokens.EndToken),
                    m_context == Context.SourceProcessing ? "+" : "*"), 
                RegexOptions.CultureInvariant 
                    | RegexOptions.Singleline);
                        // RegexOptions.Singleline in fact enable multi-line nuggets.
        }