Skip to content

Commit

Permalink
Command-line ( and ) to push/pop settings (#84)
Browse files Browse the repository at this point in the history
  • Loading branch information
edemaine committed Sep 24, 2022
1 parent 9ad7f19 commit f5a6156
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 46 deletions.
7 changes: 6 additions & 1 deletion examples/mario/Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
all: castle underground underwater overworld
all:
svgtiler -f mario.coffee \
\( -s palette=castle -O \*_castle door.tsv \) \
\( -s palette=overworld -O \*_overworld door.tsv \) \
\( -s palette=underground -O \*_underground door.tsv \) \
\( -s palette=underwater -O \*_underwater door.tsv \)

castle:
svgtiler -f -s palette=castle -O \*_castle mario.coffee door.tsv
Expand Down
77 changes: 40 additions & 37 deletions examples/mario/mario.coffee
Original file line number Diff line number Diff line change
@@ -1,45 +1,48 @@
palette = share.palette ? 'overworld'
console.log "Using Mario #{palette} palette"
palette = background = map = null

background =
switch palette
when 'castle' then 'black'
when 'overworld' then '#6b8cff'
when 'underground' then 'black'
when 'underwater' then '#0059ff'
svgtiler.beforeRender ->
palette = share.palette ? 'overworld'
console.log "Using Mario #{palette} palette"

map =
'': <symbol viewBox="0 0 16 16">
<rect width="16" height="16" fill={background}/>
</symbol>
background =
switch palette
when 'castle' then 'black'
when 'overworld' then '#6b8cff'
when 'underground' then 'black'
when 'underwater' then '#0059ff'

# Environment
# These are <symbol>s instead of filename strings to enable building below.
brick: <symbol viewBox="0 0 16 16"><image xlink:href="brick_#{palette}.png"/></symbol>
brick_lit: <symbol viewBox="0 0 16 16"><image xlink:href="brick_lit_#{palette}.png"/></symbol>
question: <symbol viewBox="0 0 16 16"><image xlink:href="question_#{palette}.png"/></symbol>
question_used: <symbol viewBox="0 0 16 16"><image xlink:href="question_used_#{palette}.png"/></symbol>
raised: <symbol viewBox="0 0 16 16"><image xlink:href="raised_#{palette}.png"/></symbol>
rock: <symbol viewBox="0 0 16 16"><image xlink:href="rock_#{palette}.png"/></symbol>
fire_nw: <symbol viewBox="0 0 16 16"><image xlink:href="fire_nw.png"/></symbol>
fire_ne: <symbol viewBox="0 0 16 16"><image xlink:href="fire_ne.png"/></symbol>
fire_sw: <symbol viewBox="0 0 16 16"><image xlink:href="fire_sw.png"/></symbol>
fire_se: <symbol viewBox="0 0 16 16"><image xlink:href="fire_se.png"/></symbol>
map =
'': <symbol viewBox="0 0 16 16">
<rect width="16" height="16" fill={background}/>
</symbol>

# Players
mario_small_left: <symbol viewBox="0 0 16 16" z-index="2"><image y="1" xlink:href="mario_small_left.png"/></symbol>
mario_small_right: <symbol viewBox="0 0 16 16" z-index="2"><image y="1" xlink:href="mario_small_right.png"/></symbol>
mario_large_left: <symbol viewBox="0 0 16 16" z-index="2"><image y="-15" xlink:href="mario_large_left.png"/></symbol>
mario_large_right: <symbol viewBox="0 0 16 16" z-index="2"><image y="-15" xlink:href="mario_large_right.png"/></symbol>
luigi_small_left: <symbol viewBox="0 0 16 16" z-index="2"><image y="1" xlink:href="luigi_small_left.png"/></symbol>
luigi_small_right: <symbol viewBox="0 0 16 16" z-index="2"><image y="1" xlink:href="luigi_small_right.png"/></symbol>
luigi_large_left: <symbol viewBox="0 0 16 16" z-index="2"><image y="-15" xlink:href="luigi_large_left.png"/></symbol>
luigi_large_right: <symbol viewBox="0 0 16 16" z-index="2"><image y="-15" xlink:href="luigi_large_right.png"/></symbol>
# Environment
# These are <symbol>s instead of filename strings to enable building below.
brick: <symbol viewBox="0 0 16 16"><image xlink:href="brick_#{palette}.png"/></symbol>
brick_lit: <symbol viewBox="0 0 16 16"><image xlink:href="brick_lit_#{palette}.png"/></symbol>
question: <symbol viewBox="0 0 16 16"><image xlink:href="question_#{palette}.png"/></symbol>
question_used: <symbol viewBox="0 0 16 16"><image xlink:href="question_used_#{palette}.png"/></symbol>
raised: <symbol viewBox="0 0 16 16"><image xlink:href="raised_#{palette}.png"/></symbol>
rock: <symbol viewBox="0 0 16 16"><image xlink:href="rock_#{palette}.png"/></symbol>
fire_nw: <symbol viewBox="0 0 16 16"><image xlink:href="fire_nw.png"/></symbol>
fire_ne: <symbol viewBox="0 0 16 16"><image xlink:href="fire_ne.png"/></symbol>
fire_sw: <symbol viewBox="0 0 16 16"><image xlink:href="fire_sw.png"/></symbol>
fire_se: <symbol viewBox="0 0 16 16"><image xlink:href="fire_se.png"/></symbol>

# Enemies
goomba: <symbol viewBox="0 0 16 16" z-index="1"><image y="1" xlink:href={"goomba_#{palette}".png}/></symbol>
spiny_left: <symbol viewBox="0 0 16 16" z-index="1"><image y="1" xlink:href="spiny_left.png"/></symbol>
spiny_right: <symbol viewBox="0 0 16 16" z-index="1"><image y="1" xlink:href="spiny_right.png"/></symbol>
# Players
mario_small_left: <symbol viewBox="0 0 16 16" z-index="2"><image y="1" xlink:href="mario_small_left.png"/></symbol>
mario_small_right: <symbol viewBox="0 0 16 16" z-index="2"><image y="1" xlink:href="mario_small_right.png"/></symbol>
mario_large_left: <symbol viewBox="0 0 16 16" z-index="2"><image y="-15" xlink:href="mario_large_left.png"/></symbol>
mario_large_right: <symbol viewBox="0 0 16 16" z-index="2"><image y="-15" xlink:href="mario_large_right.png"/></symbol>
luigi_small_left: <symbol viewBox="0 0 16 16" z-index="2"><image y="1" xlink:href="luigi_small_left.png"/></symbol>
luigi_small_right: <symbol viewBox="0 0 16 16" z-index="2"><image y="1" xlink:href="luigi_small_right.png"/></symbol>
luigi_large_left: <symbol viewBox="0 0 16 16" z-index="2"><image y="-15" xlink:href="luigi_large_left.png"/></symbol>
luigi_large_right: <symbol viewBox="0 0 16 16" z-index="2"><image y="-15" xlink:href="luigi_large_right.png"/></symbol>

# Enemies
goomba: <symbol viewBox="0 0 16 16" z-index="1"><image y="1" xlink:href={"goomba_#{palette}".png}/></symbol>
spiny_left: <symbol viewBox="0 0 16 16" z-index="1"><image y="1" xlink:href="spiny_left.png"/></symbol>
spiny_right: <symbol viewBox="0 0 16 16" z-index="1"><image y="1" xlink:href="spiny_right.png"/></symbol>

export default build = (key) ->
### Keys:
Expand Down
1 change: 1 addition & 0 deletions examples/test/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ all:
svgtiler -f css-escape.css css-escape.txt css-escape.asc
svgtiler -f set.coffee set.asc
svgtiler -f share-provider.js share-user.js
svgtiler -f \( -s data=Success\! share-user.js \) share-undefined.js
4 changes: 4 additions & 0 deletions examples/test/share-undefined.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
if (share.data == null)
console.log('Success!!');
else
console.log('FAILURE!!');
40 changes: 32 additions & 8 deletions src/svgtiler.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,16 @@ defaultSettings =
keepParent: false
keepClass: false
## Major state
mappings: null # should be Mappings instance
styles: null # should be Styles instance

mappings: null # should be valid argument to Mappings.from
styles: null # should be valid argument to Styles.from

cloneSettings = (settings) ->
settings = {...settings}
if settings.mappings?
settings.mappings = Mappings.from settings.mappings, true
if settings.styles?
settings.styles = Styles.from settings.styles, true
settings
getSetting = (settings, key) ->
settings?[key] ? defaultSettings[key]
getOutputDir = (settings, extension) ->
Expand Down Expand Up @@ -887,11 +894,11 @@ class ArrayWrapper extends Array
For example, `Styles` is like an array of `Style`s; and
`Mappings` is like an array of `Mapping`s.
###
@from: (data) ->
@from: (data, clone) ->
###
Enforce `data` to be `ArrayWrapper` (sub)class.
Supported formats:
* `ArrayWrapper` (do nothing)
* `ArrayWrapper` (do nothing, unless clone requested)
* `@itemClass` (wrap in singleton)
* raw data to pass to `new @itemClass`
* `Array` of `@itemClass`
Expand All @@ -900,7 +907,10 @@ class ArrayWrapper extends Array
* `undefined`/`null` (empty)
###
if data instanceof @
data
if clone
new @ ...data
else
data
else if data?
data = [data] unless Array.isArray data
new @ ...(
Expand Down Expand Up @@ -1962,6 +1972,8 @@ main = (args = process.argv[2..]) ->
mappings: new Mappings
styles: new Styles
}
settingsStack = []
shareStack = [{}]
for arg, i in args
if skip
skip--
Expand Down Expand Up @@ -1992,7 +2004,8 @@ main = (args = process.argv[2..]) ->
when '-s', '--share'
skip = 1
[key, ...value] = args[i+1].split '='
globalShare[key] = value.join '='
shareStack.at(-1)[key] = globalShare[key] # save old value
globalShare[key] = value.join '=' # ignore later =s
when '-o', '--output'
skip = 1
settings.outputDir = args[i+1]
Expand Down Expand Up @@ -2033,6 +2046,16 @@ main = (args = process.argv[2..]) ->
settings.jobs = arg
else
console.warn "Invalid argument to --jobs: #{args[i+1]}"
when '('
shareStack.push {}
settingsStack.push settings
settings = cloneSettings settings
when ')'
if settingsStack.length
settings = settingsStack.pop()
Object.assign globalShare, shareStack.pop()
else
console.warn "Unmatched ')'"
else
files++
console.log '*', arg
Expand Down Expand Up @@ -2070,7 +2093,8 @@ svgtiler = {
id: globalId, def: globalDef,
Context, getContext, getContextString, runWithContext,
SVGTilerError, SVGNS, XLINKNS, escapeId,
main, renderDOM, defaultSettings, getSettings, convert,
main, renderDOM, convert,
defaultSettings, getSettings, cloneSettings, getSetting, getOutputDir,
static: wrapStatic,
share: globalShare
version: metadata.version
Expand Down

0 comments on commit f5a6156

Please sign in to comment.