Merge branch 'lahwaacz-var_replacements'
This commit is contained in:
commit
df1806ef4e
@ -21,6 +21,7 @@
|
|||||||
|
|
||||||
import collections
|
import collections
|
||||||
import traceback
|
import traceback
|
||||||
|
import re
|
||||||
|
|
||||||
from PyQt5.QtCore import pyqtSlot, QUrl, QObject
|
from PyQt5.QtCore import pyqtSlot, QUrl, QObject
|
||||||
|
|
||||||
@ -50,26 +51,32 @@ def _current_url(tabbed_browser):
|
|||||||
def replace_variables(win_id, arglist):
|
def replace_variables(win_id, arglist):
|
||||||
"""Utility function to replace variables like {url} in a list of args."""
|
"""Utility function to replace variables like {url} in a list of args."""
|
||||||
variables = {
|
variables = {
|
||||||
'{url}': lambda: _current_url(tabbed_browser).toString(
|
'url': lambda: _current_url(tabbed_browser).toString(
|
||||||
QUrl.FullyEncoded | QUrl.RemovePassword),
|
QUrl.FullyEncoded | QUrl.RemovePassword),
|
||||||
'{url:pretty}': lambda: _current_url(tabbed_browser).toString(
|
'url:pretty': lambda: _current_url(tabbed_browser).toString(
|
||||||
QUrl.RemovePassword),
|
QUrl.RemovePassword),
|
||||||
'{clipboard}': utils.get_clipboard,
|
'clipboard': utils.get_clipboard,
|
||||||
'{primary}': lambda: utils.get_clipboard(selection=True),
|
'primary': lambda: utils.get_clipboard(selection=True),
|
||||||
}
|
}
|
||||||
values = {}
|
values = {}
|
||||||
args = []
|
args = []
|
||||||
tabbed_browser = objreg.get('tabbed-browser', scope='window',
|
tabbed_browser = objreg.get('tabbed-browser', scope='window',
|
||||||
window=win_id)
|
window=win_id)
|
||||||
|
|
||||||
|
def repl_cb(matchobj):
|
||||||
|
"""Return replacement for given match."""
|
||||||
|
var = matchobj.group("var")
|
||||||
|
if var not in values:
|
||||||
|
values[var] = variables[var]()
|
||||||
|
return values[var]
|
||||||
|
repl_pattern = re.compile("{(?P<var>" + "|".join(variables.keys()) + ")}")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
for arg in arglist:
|
for arg in arglist:
|
||||||
for var, func in variables.items():
|
# using re.sub with callback function replaces all variables in a
|
||||||
if var in arg:
|
# single pass and avoids expansion of nested variables (e.g.
|
||||||
if var not in values:
|
# "{url}" from clipboard is not expanded)
|
||||||
values[var] = func()
|
args.append(repl_pattern.sub(repl_cb, arg))
|
||||||
arg = arg.replace(var, values[var])
|
|
||||||
args.append(arg)
|
|
||||||
except utils.ClipboardError as e:
|
except utils.ClipboardError as e:
|
||||||
raise cmdexc.CommandError(e)
|
raise cmdexc.CommandError(e)
|
||||||
return args
|
return args
|
||||||
|
@ -551,3 +551,11 @@ Feature: Various utility commands.
|
|||||||
And I put "foo" into the clipboard
|
And I put "foo" into the clipboard
|
||||||
And I run :message-info {clipboard}bar{url}
|
And I run :message-info {clipboard}bar{url}
|
||||||
Then the message "foobarhttp://localhost:*/hello.txt" should be shown
|
Then the message "foobarhttp://localhost:*/hello.txt" should be shown
|
||||||
|
|
||||||
|
@xfail_norun
|
||||||
|
Scenario: {url} in clipboard should not be expanded
|
||||||
|
When I open data/hello.txt
|
||||||
|
# FIXME: {url} should be escaped, otherwise it is replaced before it enters clipboard
|
||||||
|
And I put "{url}" into the clipboard
|
||||||
|
And I run :message-info {clipboard}bar{url}
|
||||||
|
Then the message "{url}barhttp://localhost:*/hello.txt" should be shown
|
||||||
|
Loading…
Reference in New Issue
Block a user