Skip to content
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

replace opts with logging #109

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ node_modules
coverage
t.*
tt.*
*.log
5 changes: 3 additions & 2 deletions lib/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 22 additions & 7 deletions lib/replace.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/index.ls
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ run = ({
line-diff = a-start.line - b-start.line
if line-diff is 0 then a-start.column - b-start.column else line-diff

search = (name, input) !->
search = (name, input, opts = {}) !->
console.time "search-total:#name" if debug

clean-input = input.replace /^#!.*\n/ ''
Expand All @@ -182,7 +182,7 @@ run = ({

if replacement?
try
replaced = replace replacement, clean-input, sliced-results, query-engine
replaced = replace replacement, clean-input, sliced-results, query-engine, opts
if options.to or options.in-place
results-format := 'pairs'
out [name, replaced]
Expand Down
25 changes: 18 additions & 7 deletions src/replace.ls
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ filter-regex = //
((?:\s+(?:'(?:\\'|[^'])*'|"(?:\\"|[^"])*"|[^\|\s]+))*)
//

replacer = (input, node, query-engine) ->
replacer = (input, node, query-engine, opts = {}) ->
log = (...args) ->
console.log(...args) if opts.log

(, replacement-arg) ->
if /^\s*\|\s+/.test replacement-arg
orig-results = [node]
Expand All @@ -30,9 +33,9 @@ replacer = (input, node, query-engine) ->
orig-results = [].concat that
else
try
orig-results = query-engine.query selector, node
orig-results = query-engine.query selector, node, opts
catch
orig-results = query-engine.query replacement-arg, node
orig-results = query-engine.query replacement-arg, node, opts
filters := []
if orig-results.length
results = orig-results
Expand All @@ -55,18 +58,26 @@ replacer = (input, node, query-engine) ->
switch filter-name
| 'join' =>
join := if args.length then "#{args.0}" else ''

| 'before' =>
raw-prepend := "#{args.0}#raw-prepend"

| 'after' =>
raw-append += "#{args.0}"

| 'wrap' =>
[pre, post] = if args.length is 1 then [args.0, args.0] else args
raw-prepend := "#pre#raw-prepend"
raw-append += "#post"

| 'prepend' =>
log "prepend: #args"
for arg in args then results.unshift type: 'Raw', raw: "#arg"

| 'append' =>
log "append: #args"
for arg in args then results.push type: 'Raw', raw: "#arg"

| 'each' =>
throw new Error "No arguments supplied for 'each #{args.0}'" if args.length < 2
switch args.0
Expand Down Expand Up @@ -139,7 +150,7 @@ replacer = (input, node, query-engine) ->
else
''

get-replacement-func = (replacement, input, query-engine) ->
get-replacement-func = (replacement, input, query-engine, opts) ->
if typeof! replacement is 'Function'
(node) ->
replacement do
Expand All @@ -152,15 +163,15 @@ get-replacement-func = (replacement, input, query-engine) ->
(node) ->
replacement-prime
.replace /{{}}/g, -> get-raw input, node # func b/c don't want to call get-raw unless we need to
.replace /{{((?:[^}]|}[^}])+)}}/g, replacer input, node, query-engine
.replace /{{((?:[^}]|}[^}])+)}}/g, replacer input, node, query-engine, opts

replace = (replacement, input, nodes, query-engine) ->
replace = (replacement, input, nodes, query-engine, opts) ->
input-lines = lines input
col-offset = 0
line-offset = 0
last-line = null
prev-node = end: 0
replace-node = get-replacement-func replacement, input, query-engine
replace-node = get-replacement-func replacement, input, query-engine, opts

for node in nodes
continue if node.start < prev-node.end
Expand Down