puts {
<!doctype html>
<html>
  <head>
    <title>Roguemage/комнаты</title>
    <link href="rooms.css" rel="stylesheet">
  </head>
  <body>
    <div class="container">
}

proc h2/   {class content} { return "<h2 class='$class'>$content</h2>"}
proc p/    {class content} { return "<p class='$class'>$content</p>"}
proc span/ {class content} { return "<span class='$class'>$content</span>" }
proc div/  {class content} { return "<div class='$class'>$content</div>" }
proc img/  {src attr} { return "<img src='img/$src' $attr/>" }
proc icon/ {name} { return [img/ ${name}.png {class="price__el" width="20"}] }

proc э {{times 1}} { return [string repeat [icon/ energy] $times] }
proc м {{times 1}} { return [string repeat [icon/ mana] $times] }
proc ж {{times 1}} { return [string repeat [icon/ life] $times] }

proc price {type pricestr} {
  set buf {}
  while 1 {
    if {[string match {[0-9][эмж]*} $pricestr]} {
      append buf "[[string index $pricestr 1] [string index $pricestr 0]]"
      set pricestr [string replace $pricestr 0 1]
    } else {
      if {0 != [string length $pricestr]} {
        append buf "\n    [span/ price__text $pricestr]"
      }
      break
    }
  }
  return $buf
}

proc identity {x} {return $x}

proc room_walls_from {wall_letters} {
  set classes {}
  foreach wall_letter [split $wall_letters {}] {
    # Letters are chosen based on my letter layout on Sagaris:
    # в р п
    # н т с
    append classes [switch $wall_letter {
      н {identity corridor_left}
      т {identity corridor_bottom}
      с {identity corridor_right}
      р {identity corridor_top}
    } ] " "
  }
  return $classes
}

proc room_class_from {line} {
  switch [string trimleft $line "!"] {
    "водная комната" {return "room_watery"}
    "бродячий" {return "creature creature_wandering"}
    "злой" {return "creature creature_evil"}
    "добрый" {return "creature creature_good"}
    default {puts "Unknown room class "$line}
  }
}

set card_cound 0
set flavor ""
set state {top of card}
set card_text_buf {}
set room_classes [list]
foreach line [split [subst [read stdin]] "\n"] {
  if {[string match {#*} $line]} {
    # These lines are ignored. Get advantage of them.
  } elseif {[string match {!*} $line]} {
    # These are lines that set some information about the room.
    # Basically, setting a css class.
    lappend room_classes [room_class_from $line]
  } elseif {(0 == [string length $line])} {
    incr card_count
    if {$card_count % 9 == 0} {
      lappend room_classes "nineth"
    }
    # End of card.
    puts [div/ "card [join $room_classes]" "$card_text_buf"]
    set card_text_buf {}
    set room_classes [list]
    set state {top of card}
  } elseif {[string equal $state {top of card}] && [string match *:* $line]} {
    # First line of card.
    foreach {lhs rhs} [split $line :] {
      lappend room_classes [room_walls_from $lhs]
      append card_text_buf [h2/ room__name $rhs]
    }
    set state {in card}
  } elseif {[string equal $state {in card}] && [string match *:* $line]} {
    foreach {lhs rhs} [split $line :] {
      append card_text_buf\
        [div/ ability [price ability $lhs][span/ ability__text ": $rhs"]]
    }
  } elseif {[string match {>*} $line]} {
    append card_text_buf [p/ {flavor} [string range $line 1 [string length $line]]]
  } else {
    append card_text_buf [p/ {} $line]
  }
}

puts {
  </div>
  </body>
  </html>
}
