-
-
Notifications
You must be signed in to change notification settings - Fork 325
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Starlark #341
Open
orsinium
wants to merge
14
commits into
muesli:master
Choose a base branch
from
orsinium-forks:starlark
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Starlark #341
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
4deddf1
improve filters interface
orsinium 4979601
+dedent
orsinium 5514b6b
+starlark filter
orsinium f0b8fc1
+starlark dep
orsinium 152d9c6
make starlark selectable
orsinium 4dfb026
reflect
orsinium 3fdd3b6
fix assignment scope
orsinium 7028380
register starlark
orsinium fb1bc11
better error when cannot find filter
orsinium a070c42
better unknown type error message
orsinium cb2241a
support map for starlark
orsinium e0dd998
be explicit when no bee found
orsinium 7835790
support pointers for starlark
orsinium 2664a6a
+docs
orsinium File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# Filters | ||
|
||
Whenever an Event occurs, all of the Filters you defined in a Chain get executed with the data the Event provided. Only if the Event passes all of the Filters, the configured Actions in this Chain get then executed. | ||
|
||
At the moment, there are 2 different types of filters: | ||
|
||
+ temmplate | ||
+ starlark | ||
|
||
## Template | ||
|
||
"Template" filter type uses [text/template](https://golang.org/pkg/text/template/) Go package to execute filters. Beehive exposes in the template a few helper functions and most of the [strings](https://golang.org/pkg/strings/) package functions to make templates more powerful. Also an important thing is that the template for filters should go inside `{{test ...}}`. That's pretty much it. | ||
|
||
For example, let's check if `text` contains word "beehive": | ||
|
||
```clojure | ||
{{test Contains (ToLower .text) "beehive"}} | ||
``` | ||
|
||
See [Beehive wiki](https://github.com/muesli/beehive/wiki/Filters) for more information. | ||
|
||
## Starlark | ||
|
||
[Starlark](https://github.com/bazelbuild/starlark) is a dialect of Python created for [Bazel](https://bazel.build/) configuration files. If you know Python, you already know Starlark. To make sure if a syntax feature is supported check [the specification](https://github.com/google/starlark-go/blob/master/doc/spec.md). | ||
|
||
Beehive executes `main` function from the filter, passing all variables inside as keyword arguments. The function must return a boolean result. | ||
|
||
For example, let's check if title or description of an RSS feed contains any of the given terms: | ||
|
||
```python | ||
def main(title, description, **kwargs): | ||
text = title + " " + description | ||
text = text.lower() | ||
terms = ["beehive", "muesli"] | ||
for term in terms: | ||
if term in text: | ||
return True | ||
return False | ||
``` | ||
|
||
"Starlark" filters are more verbose that "template" but much more powerful and turing-complete. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// https://github.com/lithammer/dedent | ||
package starlarkfilter | ||
|
||
import ( | ||
"regexp" | ||
"strings" | ||
) | ||
|
||
var ( | ||
whitespaceOnly = regexp.MustCompile("(?m)^[ \t]+$") | ||
leadingWhitespace = regexp.MustCompile("(?m)(^[ \t]*)(?:[^ \t\n])") | ||
) | ||
|
||
// Dedent removes any common leading whitespace from every line in text. | ||
// | ||
// This can be used to make multiline strings to line up with the left edge of | ||
// the display, while still presenting them in the source code in indented | ||
// form. | ||
func dedent(text string) string { | ||
var margin string | ||
|
||
text = whitespaceOnly.ReplaceAllString(text, "") | ||
indents := leadingWhitespace.FindAllStringSubmatch(text, -1) | ||
|
||
// Look for the longest leading string of spaces and tabs common to all | ||
// lines. | ||
for i, indent := range indents { | ||
if i == 0 { | ||
margin = indent[1] | ||
} else if strings.HasPrefix(indent[1], margin) { | ||
// Current line more deeply indented than previous winner: | ||
// no change (previous winner is still on top). | ||
continue | ||
} else if strings.HasPrefix(margin, indent[1]) { | ||
// Current line consistent with and no deeper than previous winner: | ||
// it's the new winner. | ||
margin = indent[1] | ||
} else { | ||
// Current line and previous winner have no common whitespace: | ||
// there is no margin. | ||
margin = "" | ||
break | ||
} | ||
} | ||
|
||
if margin != "" { | ||
text = regexp.MustCompile("(?m)^"+margin).ReplaceAllString(text, "") | ||
} | ||
return text | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,179 @@ | ||
// https://github.com/lithammer/dedent/blob/master/dedent_test.go | ||
package starlarkfilter | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
) | ||
|
||
const errorMsg = "\nexpected %q\ngot %q" | ||
|
||
type dedentTest struct { | ||
text, expect string | ||
} | ||
|
||
func TestDedentNoMargin(t *testing.T) { | ||
texts := []string{ | ||
// No lines indented | ||
"Hello there.\nHow are you?\nOh good, I'm glad.", | ||
// Similar with a blank line | ||
"Hello there.\n\nBoo!", | ||
// Some lines indented, but overall margin is still zero | ||
"Hello there.\n This is indented.", | ||
// Again, add a blank line. | ||
"Hello there.\n\n Boo!\n", | ||
} | ||
|
||
for _, text := range texts { | ||
if text != dedent(text) { | ||
t.Errorf(errorMsg, text, dedent(text)) | ||
} | ||
} | ||
} | ||
|
||
func TestDedentEven(t *testing.T) { | ||
texts := []dedentTest{ | ||
{ | ||
// All lines indented by two spaces | ||
text: " Hello there.\n How are ya?\n Oh good.", | ||
expect: "Hello there.\nHow are ya?\nOh good.", | ||
}, | ||
{ | ||
// Same, with blank lines | ||
text: " Hello there.\n\n How are ya?\n Oh good.\n", | ||
expect: "Hello there.\n\nHow are ya?\nOh good.\n", | ||
}, | ||
{ | ||
// Now indent one of the blank lines | ||
text: " Hello there.\n \n How are ya?\n Oh good.\n", | ||
expect: "Hello there.\n\nHow are ya?\nOh good.\n", | ||
}, | ||
} | ||
|
||
for _, text := range texts { | ||
if text.expect != dedent(text.text) { | ||
t.Errorf(errorMsg, text.expect, dedent(text.text)) | ||
} | ||
} | ||
} | ||
|
||
func TestDedentUneven(t *testing.T) { | ||
texts := []dedentTest{ | ||
{ | ||
// Lines indented unevenly | ||
text: ` | ||
def foo(): | ||
while 1: | ||
return foo | ||
`, | ||
expect: ` | ||
def foo(): | ||
while 1: | ||
return foo | ||
`, | ||
}, | ||
{ | ||
// Uneven indentation with a blank line | ||
text: " Foo\n Bar\n\n Baz\n", | ||
expect: "Foo\n Bar\n\n Baz\n", | ||
}, | ||
{ | ||
// Uneven indentation with a whitespace-only line | ||
text: " Foo\n Bar\n \n Baz\n", | ||
expect: "Foo\n Bar\n\n Baz\n", | ||
}, | ||
} | ||
|
||
for _, text := range texts { | ||
if text.expect != dedent(text.text) { | ||
t.Errorf(errorMsg, text.expect, dedent(text.text)) | ||
} | ||
} | ||
} | ||
|
||
// dedent() should not mangle internal tabs. | ||
func TestDedentPreserveInternalTabs(t *testing.T) { | ||
text := " hello\tthere\n how are\tyou?" | ||
expect := "hello\tthere\nhow are\tyou?" | ||
if expect != dedent(text) { | ||
t.Errorf(errorMsg, expect, dedent(text)) | ||
} | ||
|
||
// Make sure that it preserves tabs when it's not making any changes at all | ||
if expect != dedent(expect) { | ||
t.Errorf(errorMsg, expect, dedent(expect)) | ||
} | ||
} | ||
|
||
// dedent() should not mangle tabs in the margin (i.e. tabs and spaces both | ||
// count as margin, but are *not* considered equivalent). | ||
func TestDedentPreserveMarginTabs(t *testing.T) { | ||
texts := []string{ | ||
" hello there\n\thow are you?", | ||
// Same effect even if we have 8 spaces | ||
" hello there\n\thow are you?", | ||
} | ||
|
||
for _, text := range texts { | ||
d := dedent(text) | ||
if text != d { | ||
t.Errorf(errorMsg, text, d) | ||
} | ||
} | ||
|
||
texts2 := []dedentTest{ | ||
{ | ||
// dedent() only removes whitespace that can be uniformly removed! | ||
text: "\thello there\n\thow are you?", | ||
expect: "hello there\nhow are you?", | ||
}, | ||
{ | ||
text: " \thello there\n \thow are you?", | ||
expect: "hello there\nhow are you?", | ||
}, | ||
{ | ||
text: " \t hello there\n \t how are you?", | ||
expect: "hello there\nhow are you?", | ||
}, | ||
{ | ||
text: " \thello there\n \t how are you?", | ||
expect: "hello there\n how are you?", | ||
}, | ||
} | ||
|
||
for _, text := range texts2 { | ||
if text.expect != dedent(text.text) { | ||
t.Errorf(errorMsg, text.expect, dedent(text.text)) | ||
} | ||
} | ||
} | ||
|
||
func Examplededent() { | ||
s := ` | ||
Lorem ipsum dolor sit amet, | ||
consectetur adipiscing elit. | ||
Curabitur justo tellus, facilisis nec efficitur dictum, | ||
fermentum vitae ligula. Sed eu convallis sapien.` | ||
fmt.Println(dedent(s)) | ||
fmt.Println("-------------") | ||
fmt.Println(s) | ||
// Output: | ||
// Lorem ipsum dolor sit amet, | ||
// consectetur adipiscing elit. | ||
// Curabitur justo tellus, facilisis nec efficitur dictum, | ||
// fermentum vitae ligula. Sed eu convallis sapien. | ||
// ------------- | ||
// | ||
// Lorem ipsum dolor sit amet, | ||
// consectetur adipiscing elit. | ||
// Curabitur justo tellus, facilisis nec efficitur dictum, | ||
// fermentum vitae ligula. Sed eu convallis sapien. | ||
} | ||
|
||
func BenchmarkDedent(b *testing.B) { | ||
for i := 0; i < b.N; i++ { | ||
dedent(`Lorem ipsum dolor sit amet, consectetur adipiscing elit. | ||
Curabitur justo tellus, facilisis nec efficitur dictum, | ||
fermentum vitae ligula. Sed eu convallis sapien.`) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not too happy with this "string magic". Maybe we could introduce proper filter types? I was also pondering having simple bash-scripts as filters, which either return 0 or 1 as a result.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm also not sure if it is a good solution but it requires a selector on the UI side and I'm not ready to dive into Ember. If you're ready to do such adjustments, please, do.
If to talk about a better backend-only solution, I can propose shebangs. In that case, this line will be
if strings.HasPrefix(source, "#!/usr/bin/env starlark")
, for example.Bash scripts is a cool and fun idea. I'm sure some will be happy to build grep/sed/awk/perl pipelines.