nebius/SWE-rebench-V2-PRs huggingface dataset:
Repository: mozilla/nunjucks
Pull request #476 (2015-07-05 12:27:08)

Add getGlobal() function

garygreen: Do you have a use case for this?
atorkhov: Yes, I'm doing unit tests and checking custom filters and global functions behaviour. To get filters I can use getFilter(), but for globals I need to expose them in my application somehow.
garygreen: Ok cool, would you mind adding some tests in `tests/global.js` for this?

Patch:
diff --git a/docs/api.md b/docs/api.md
index b211895..2f322c0 100644
--- a/docs/api.md
+++ b/docs/api.md
@@ -261,6 +261,12 @@ env.addGlobal(name, value)
 Add a global value that will be available to all templates. Note: this will overwrite any existing global called `name`.
 {% endapi %}
 
+{% api %}
+getGlobal
+env.getGlobal(name)
+Get a global named **name**.
+{% endapi %}
+
 {% api %}
 getTemplate
 env.getTemplate(name, [eagerCompile], [callback])
diff --git a/src/environment.js b/src/environment.js
index 0b29b5a..83d17e3 100644
--- a/src/environment.js
+++ b/src/environment.js
@@ -107,6 +107,13 @@ var Environment = Obj.extend({
         globals[name] = value;
     },
 
+    getGlobal: function(name) {
+        if(!globals[name]) {
+            throw new Error('global not found: ' + name);
+        }
+        return globals[name];
+    },
+
     addFilter: function(name, func, async) {
         var wrapped = func;
 


Test patch:
diff --git a/tests/globals.js b/tests/globals.js
index 227b051..0b355bb 100644
--- a/tests/globals.js
+++ b/tests/globals.js
@@ -84,6 +84,27 @@
           finish(done);
         });
 
+        it('should allow getting of globals', function(done) {
+            var env = new Environment(new Loader(templatesPath));
+            var hello = function(arg1) {
+                return 'Hello ' + arg1;
+            };
+
+            env.addGlobal('hello', hello);
+
+            expect(env.getGlobal('hello')).to.be.equal(hello);
+
+            finish(done);
+        });
+
+        it('should fail on getting non-existent global', function(done) {
+            var env = new Environment(new Loader(templatesPath));
+
+            expect(env.getGlobal).withArgs('hello1').to.throwError();
+
+            finish(done);
+        });
+
         it('should pass context as this to global functions', function(done) {
             var env = new Environment(new Loader(templatesPath));
Repository: mozilla/nunjucks
Pull request #503 (2015-09-01 10:28:17)

Fix/multiple raw
Improves new raw block logic (more robust)

### Fixes:
* Greedy raw matching that broke multiple raw blocks

Fixes #502 


Patch:
diff --git a/src/parser.js b/src/parser.js
index 2806c16..5835f49 100644
--- a/src/parser.js
+++ b/src/parser.js
@@ -549,7 +549,8 @@ var Parser = Object.extend({
     },
 
     parseRaw: function() {
-        var blockRegex = /([\s\S]*){%\s*(.*?)\s*(?=%})%}/;
+        // Look for upcoming raw blocks (ignore all other kinds of blocks)
+        var rawBlockRegex = /([\s\S]*?){%\s*(raw|endraw)\s*(?=%})%}/;
         var rawLevel = 1;
         var str = '';
         var matches = null;
@@ -560,14 +561,13 @@ var Parser = Object.extend({
 
         // Exit when there's nothing to match
         // or when we've found the matching "endraw" block
-        while((matches = this.tokens._extractRegex(blockRegex)) && rawLevel > 0) {
+        while((matches = this.tokens._extractRegex(rawBlockRegex)) && rawLevel > 0) {
             var all = matches[0];
             var pre = matches[1];
             var blockName = matches[2];
 
             // Adjust rawlevel
             if(blockName === 'raw') {
-
                 rawLevel += 1;
             } else if(blockName === 'endraw') {
                 rawLevel -= 1;


Test patch:
diff --git a/tests/parser.js b/tests/parser.js
index eaf0f8c..d5e58cf 100644
--- a/tests/parser.js
+++ b/tests/parser.js
@@ -459,6 +459,26 @@
                     [nodes.TemplateData, '{# test ']]]);
         });
 
+        it('should parse multiple raw blocks', function() {
+            isAST(parser.parse('{% raw %}{{ var }}{% endraw %}{{ var }}{% raw %}{{ var }}{% endraw %}'),
+                  [nodes.Root,
+                   [nodes.Output, [nodes.TemplateData, '{{ var }}']],
+                   [nodes.Output, [nodes.Symbol, 'var']],
+                   [nodes.Output, [nodes.TemplateData, '{{ var }}']]]);
+        });
+
+        it('should parse multiline multiple raw blocks', function() {
+            isAST(parser.parse('\n{% raw %}{{ var }}{% endraw %}\n{{ var }}\n{% raw %}{{ var }}{% endraw %}\n'),
+                  [nodes.Root,
+                   [nodes.Output, [nodes.TemplateData, '\n']],
+                   [nodes.Output, [nodes.TemplateData, '{{ var }}']],
+                   [nodes.Output, [nodes.TemplateData, '\n']],
+                   [nodes.Output, [nodes.Symbol, 'var']],
+                   [nodes.Output, [nodes.TemplateData, '\n']],
+                   [nodes.Output, [nodes.TemplateData, '{{ var }}']],
+                   [nodes.Output, [nodes.TemplateData, '\n']]]);
+        });
+
         it('should parse keyword and non-keyword arguments', function() {
             isAST(parser.parse('{{ foo("bar", falalalala, baz="foobar") }}'),
                   [nodes.Root,
Repository: mozilla/nunjucks
Pull request #510 (2015-09-07 16:32:14)

kwargs support for sort filter
Added support for kwargs on the sort function 

http://jinja.pocoo.org/docs/dev/templates/#sort

https://github.com/mozilla/nunjucks/issues/255


Patch:
diff --git a/src/filters.js b/src/filters.js
index 50c361c..c457727 100644
--- a/src/filters.js
+++ b/src/filters.js
@@ -373,8 +373,8 @@ var filters = {
         return res;
     },
 
-    sort: function(arr, reverse, caseSens, attr) {
-        // Copy it
+    sort: r.makeMacro(['value', 'reverse', 'case_sensitive', 'attribute'], [], function(arr, reverse, caseSens, attr) {
+         // Copy it
         arr = lib.map(arr, function(v) { return v; });
 
         arr.sort(function(a, b) {
@@ -406,7 +406,7 @@ var filters = {
         });
 
         return arr;
-    },
+    }),
 
     string: function(obj) {
         return r.copySafeness(obj, obj);


Test patch:
diff --git a/tests/filters.js b/tests/filters.js
index df0b82e..5248ab7 100644
--- a/tests/filters.js
+++ b/tests/filters.js
@@ -382,6 +382,9 @@
                                  { name: 'john' }]},
                   'fredjamesjohn');
 
+            equal('{% for i in [ {n:3},{n:5},{n:2},{n:1},{n:4},{n:6}] | sort(attribute="n") %}{{ i.n }}{% endfor %}',
+                  '123456');
+
             finish(done);
         });