Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Add regexp capturing in rules #264

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

Open
kndoo wants to merge 1 commit into ruby:master
base: master
Choose a base branch
Loading
from kndoo:add-regexp-capturing-in-rules

Conversation

@kndoo
Copy link

@kndoo kndoo commented Apr 30, 2018

This PR allows regexp capturing and backreferencing to task_pattern in rules.

rule task_pattern => source do |task|
 # action
end
  • Regexp.last_match ($~) will be set in Procs at both source and action.
    • Other last_match related special variables, such as 1ドル, $&, $`, and $', will also be set.
  • If source is a String, it may contain backreferences to task_pattern by same format as String#sub.

Here are some examples:

rule /^(.*)-(\d+)x(\d+)\.png$/ => [ # regexp with capture groups
 proc { "#{1ドル}.svg" } # capture variable in a source proc
 # or '1円.svg' # backreference in a source string
] do |task|
 basename, width, height = 1ドル, 2ドル, 3ドル # capture variables in an action proc
 sh "convert #{basename}.svg -resize #{width}x#{height} #{basename}-#{width}x#{height}.png"
end

This can make icon-32x32.png from icon.svg.

rule /-(?<width>\d+)x(?<height>\d+).(?<ext>png|jpg)$/ => [ # regexp with named capture groups
 proc { "#{$`}.#{$~[:ext]}" } # matchdata in a source proc
 # or '\`.\k<ext>' # named backreference in a source string
 # or '.\k<ext>' # file extension replacement (already implemented in the current master branch)
] do |task|
 sh "convert #{task.source} -resize #{$~[:width]}x#{$~[:height]} #{task.name}" # accessing named capture groups
end
rule %r{^build/(?<basename>.*?)\.html\.(?<lang>.*?)$} => [
 'doc/\k<lang>/\k<basename>.md' # named backreference in a source string
] do
 sh "pandoc -o #{task.name} #{task.source}"
end

This can make build/index.html.en from doc/en/index.md.

Although it passes all the tests, it will break backward compatibility in some cases:

  • Procs that refer to Regexp.last_match related variables assigned outside the rule block
  • String sources that include backslashes (directory separator in Windows?)

Former case example:

options.match(/^CFLAGS=(.*)$/) # extract a parameter from a multiline text
rule '.o' => '.c' do |task|
 sh "gcc #{1ドル} -c -o #{task.name} #{task.source} # 1ドル will be replaced to nil
end

Any feedback would be appreciated. Thanks.

chocolateboy reacted with thumbs up emoji
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

No reviews

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

1 participant

AltStyle によって変換されたページ (->オリジナル) /