Exemple #1
0
        public void FullSnippetInfoTest()
        {
            var res1 = CodeSnippetHelper.Select("line1\r\nline2", "cs", new CodeSelectorOptions());
            var res2 = CodeSnippetHelper.Select("//---reg1\r\nline1\r\nline2\r\n//---", "cs", new CodeSelectorOptions());

            Assert.AreEqual(SnippetLocation_e.Full, res1[0].Location);
            Assert.AreEqual(SnippetLocation_e.Full, res2[0].Location);
        }
Exemple #2
0
        public void HideRegions()
        {
            var res = CodeSnippetHelper.Select("line1\r\nline2\r\n    //--- Reg1\r\nline3\r\nline4\r\n    //---\r\nline5", "cs",
                                               new CodeSelectorOptions());

            Assert.AreEqual(1, res.Length);
            Assert.AreEqual("line1\r\nline2\r\nline3\r\nline4\r\nline5", res[0].Code);
        }
Exemple #3
0
        public void ExcludeSingleOnlyRegion()
        {
            var res = CodeSnippetHelper.Select("//--- Reg1\r\nline3\r\nline4\r\n    //---", "cs", new CodeSelectorOptions()
            {
                ExcludeRegions = new string[] { "Reg1" }
            });

            Assert.AreEqual(0, res.Length);
        }
Exemple #4
0
        public void SelectSingleRegionVB()
        {
            var res = CodeSnippetHelper.Select("line1\r\nline2\r\n    '--- Reg1\r\n    line3\r\n    line4\r\n    '---\r\nline5", "vb", new CodeSelectorOptions()
            {
                Regions = new string[] { "Reg1" }
            });

            Assert.AreEqual(1, res.Length);
            Assert.AreEqual("    line3\r\n    line4", res[0].Code);
        }
Exemple #5
0
 public void OpenRegionTest()
 {
     Assert.Throws <PluginUserMessageException>(() =>
     {
         var res = CodeSnippetHelper.Select("'---Reg1\r\nline1\r\n'---\r\n \r\n'--- Reg2\r\nline2\r\n'----\r\n'--- Reg3\r\nline3\r\n'---\r\nfree text", "vb", new CodeSelectorOptions()
         {
             Regions = new string[] { "Reg1", "Reg2", "Reg3" }
         });
     });
 }
Exemple #6
0
        public void BoundaryTopNestedRegionsJaggedTest()
        {
            var res1 = CodeSnippetHelper.Select("//---reg1\r\n//---reg2\r\nline2\r\n//---\r\nline1\r\n//---\r\nline3", "cs",
                                                new CodeSelectorOptions()
            {
                Regions = new string[] { "reg2" }
            });

            Assert.AreEqual(SnippetLocation_e.Start, res1[0].Location);
        }
Exemple #7
0
        public void FullRegionsJaggedTest()
        {
            var res1 = CodeSnippetHelper.Select("//---reg1\r\nline1\r\nline2\r\n//---", "cs",
                                                new CodeSelectorOptions()
            {
                Regions = new string[] { "reg1" }
            });

            Assert.AreEqual(SnippetLocation_e.Full, res1[0].Location);
        }
Exemple #8
0
        public void SelectSingleOnlyRegion()
        {
            var res = CodeSnippetHelper.Select("//--- Reg1\r\nline3\r\nline4\r\n    //---", "cs", new CodeSelectorOptions()
            {
                Regions = new string[] { "Reg1" }
            });

            Assert.AreEqual(1, res.Length);
            Assert.AreEqual("line3\r\nline4", res[0].Code);
        }
Exemple #9
0
        public void LeftAlignCode()
        {
            var res = CodeSnippetHelper.Select("line1\r\nline2\r\n    //--- Reg1\r\n    line3\r\n    line4\r\n    //---\r\nline5", "cs", new CodeSelectorOptions()
            {
                LeftAlign = true,
                Regions   = new string[] { "Reg1" }
            });

            Assert.AreEqual(1, res.Length);
            Assert.AreEqual("line3\r\nline4", res[0].Code);
        }
Exemple #10
0
        public void ExcludeSingleRegions()
        {
            var res = CodeSnippetHelper.Select("line1\r\nline2\r\n    //--- Reg1\r\nline3\r\nline4\r\n    //---\r\nline5\r\n    //--- Reg2\r\nline6\r\nline7\r\n    //---\r\nline8", "cs", new CodeSelectorOptions()
            {
                ExcludeRegions = new string[] { "Reg1" }
            });

            Assert.AreEqual(2, res.Length);
            Assert.AreEqual("line1\r\nline2", res[0].Code);
            Assert.AreEqual("line5\r\nline6\r\nline7\r\nline8", res[1].Code);
        }
Exemple #11
0
        public void SelectMidRegions()
        {
            var res = CodeSnippetHelper.Select("'---Reg1\r\nline1\r\nline2\r\n'---\r\n \r\n'--- Reg2\r\nline3\r\nline4\r\n'---\r\n \r\n'--- Reg3\r\nline5\r\nline6\r\n'---\r\nfree text", "vb", new CodeSelectorOptions()
            {
                Regions = new string[] { "Reg1", "Reg3" }
            });

            Assert.AreEqual(2, res.Length);
            Assert.AreEqual("line1\r\nline2", res[0].Code);
            Assert.AreEqual("line5\r\nline6", res[1].Code);
        }
Exemple #12
0
        public void IncludeExcludeRegionsJaggedTest()
        {
            var res1 = CodeSnippetHelper.Select("line1\r\n//---reg1\r\n//---reg2\r\nline2\r\n//---\r\nline1\r\n//---\r\nline3", "cs",
                                                new CodeSelectorOptions()
            {
                Regions        = new string[] { "reg1" },
                ExcludeRegions = new string[] { "reg2" }
            });

            Assert.AreEqual(SnippetLocation_e.Middle, res1[0].Location);
        }
Exemple #13
0
        public void SelectMultiRegionsAndExclude()
        {
            var res = CodeSnippetHelper.Select("line1\r\nline2\r\n    //--- Reg1\r\nline3\r\nline4\r\n    //---\r\nline5\r\n//--- Reg2\r\nl1\r\n    //--- Reg3\r\nline6\r\nline7\r\n    //---\r\n//---\r\nline8", "cs", new CodeSelectorOptions()
            {
                Regions        = new string[] { "Reg2" },
                ExcludeRegions = new string[] { "Reg3" }
            });

            Assert.AreEqual(1, res.Length);
            Assert.AreEqual("l1", res[0].Code);
        }
Exemple #14
0
        public void JaggedSnippetInfoTest()
        {
            var res1 = CodeSnippetHelper.Select("line1\r\n//---reg1\r\nline2\r\nline3\r\n//---\r\nline4", "cs",
                                                new CodeSelectorOptions()
            {
                Regions = new string[] { "reg1" }
            });

            var res2 = CodeSnippetHelper.Select("//---reg1\r\nline1\r\nline2\r\n//---\r\nline3\r\n//---reg2\r\nline4\r\nline5\r\n//---", "cs",
                                                new CodeSelectorOptions()
            {
                ExcludeRegions = new string[] { "reg1", "reg2" }
            });

            Assert.AreEqual(SnippetLocation_e.Middle, res1[0].Location);
            Assert.AreEqual(SnippetLocation_e.Middle, res2[0].Location);
        }
Exemple #15
0
        private async Task WriteCodeSnippet(StringBuilder html, IPage page,
                                            string filePath, string lang, string[] exclRegs, bool leftAlign, string[] regs)
        {
            IAsset snipAsset;

            try
            {
                var searchFolder = FindSnippetFolder(m_Site, page, ref filePath);

                var fileName = new PluginLocation("", Path.GetFileName(filePath), Enumerable.Empty <string>());
                snipAsset = searchFolder.FindAsset(fileName);
            }
            catch (Exception ex)
            {
                throw new NullReferenceException($"Failed to find code snippet: '{filePath}'", ex);
            }

            if (snipAsset != null)
            {
                if (!m_SnippetFileIds.Contains(snipAsset.Id))
                {
                    m_SnippetFileIds.Add(snipAsset.Id);
                }

                var rawCode = snipAsset.AsTextContent();

                if (string.IsNullOrEmpty(lang))
                {
                    lang = Path.GetExtension(snipAsset.FileName).TrimStart('.').ToLower();
                }

                var snips = CodeSnippetHelper.Select(rawCode, lang, new CodeSelectorOptions()
                {
                    ExcludeRegions = exclRegs,
                    LeftAlign      = leftAlign,
                    Regions        = regs
                });

                foreach (var snip in snips)
                {
                    var snipClass = "";

                    switch (snip.Location)
                    {
                    case SnippetLocation_e.Full:
                        snipClass = "";
                        break;

                    case SnippetLocation_e.Start:
                        snipClass = "jagged-bottom";
                        break;

                    case SnippetLocation_e.End:
                        snipClass = "jagged-top";
                        break;

                    case SnippetLocation_e.Middle:
                        snipClass = "jagged";
                        break;
                    }

                    var code = $"~~~{lang} {snipClass}\r\n{snip.Code}\r\n~~~";
                    html.AppendLine(await m_App.Compiler.StaticContentTransformer.Transform(code));
                }
            }
            else
            {
                throw new InvalidCastException($"Failed to find an asset at '{filePath}'");
            }
        }