Skip to content

Instantly share code, notes, and snippets.

@lepinekong
Last active April 21, 2018 14:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lepinekong/f007c5358104f0469d8b1ea7da11650e to your computer and use it in GitHub Desktop.
Save lepinekong/f007c5358104f0469d8b1ea7da11650e to your computer and use it in GitHub Desktop.
Red [
Title: ".system.libraries.shell.red"
Uses: [
%.system.libraries.string.red
]
]
;for remote execution
unless value? '.string.compose [
github-url-entry: https://gist.github.com/lepinekong/f007c5358104f0469d8b1ea7da11650e
lib: get-github-url github-url-entry %.system.libraries.string.red
do read lib
]
.to-powershell: function[.powershell-command [string! block!]][
either block? .powershell-command [
command: .string.compose/separator .powershell-command ";"
][
command: .powershell-command
]
replace/all command {\} {\\}
replace/all command {"} {\"}
powershell-command: rejoin [{powershell -command} { } {"} command {"}]
return powershell-command
]
.to.powershell: :.to-powershell
to-powershell: :.to-powershell
to.powershell: :.to-powershell
.call-powershell: function[.powershell-command /out /silent][
powershell-command: .to-powershell .powershell-command
either not out [
call powershell-command
][
output: copy ""
do-events/no-wait
unless silent [
print powershell-command
]
do-events/no-wait
call/output powershell-command output
unless silent [
print output
]
return output
]
]
call-powershell: :.Call-Powershell
Red [
title: ".system.libraries.string.red"
uses: [
%.system.libraries.string.template.red
]
]
unless try [.do %.system.libraries.string.template.red][]
;for remote execution
unless value? 'build-markup [
github-url-entry: https://gist.github.com/lepinekong/f007c5358104f0469d8b1ea7da11650e
lib: get-github-url github-url-entry %.system.libraries.string.template.red
do read lib
]
.string.compose: function [.string-block [block!] /separator .separator][
{
#### Example:
- [x] [1]. .string.compose ["a" "b" "c"]
}
string-block: copy []
either separator [
append string-block .string-block/1
foreach string skip .string-block 1 [
append string-block .separator
append string-block string
]
][
string-block: copy .string-block
]
return rejoin compose string-block
]
string.compose: :.string.compose
compose-string: :string.compose
.string.expand: function[.string-template [string!] .block-vars[block!]][
return build-markup/bind .string-template Context Compose .block-vars
]
string-expand: :.string.expand
.expand: :.string.expand
.string.trim: function [.string][
return trim/head/tail .string
]
.trim: :.string.trim
.string.start.with: function[_url _prefix][
url: to-string _url
prefix: _prefix
found: find url prefix
if none? found [
return false
]
either (index? found) = 1 [
return true
][
return false
]
]
string.start.with: :.string.start.with
string.start-with: :.string.start.with
.start.with: :.string.start.with
start.with: :.string.start.with
start-with: :.string.start.with
.string.contains: function [.full-string .searched-string][
either find .full-string .searched-string [
return true
][
return false
]
]
.pad-left: function [.string [string! number!] n [integer!]][
string: form .string
pad/left/with string n #"0"
]
pad-left: :.pad-left
.replace: function [
series [series! none!]
pattern
value
/all
][
if error? try [
either all [
replace/all series pattern value
return series
][
replace series pattern value
return series
]
][
return none
]
]
Red [
Title: ".system.libraries.string.template.red"
]
.build-markup: func [
{Return markup text replacing <%tags%> with their evaluated results.}
content [string! file! url!]
/bind obj [object!] "Object to bind" ;ability to run in a local context
/quiet "Do not show errors in the output."
/local out eval value
][
content: either string? content [copy content] [read content]
out: make string! 126
eval: func [val /local tmp] [
either error? set/any 'tmp try [either bind [do system/words/bind load val obj] [do val]] [
if not quiet [
tmp: disarm :tmp
append out reform ["***ERROR" tmp/id "in:" val]
]
] [
if not unset? get/any 'tmp [append out :tmp]
]
]
parse content [
any [
end break
| "<%" [copy value to "%>" 2 skip | copy value to end] (eval value)
| copy value [to "<%" | to end] (append out value)
]
]
out
]
build-markup: :.build-markup
.get-vars: function[template][
vars: copy []
rules: [any [thru "<%" copy var to "%>" (append vars var)]]
parse template rules
return vars
]
get-vars: :.get-vars
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment