ActiveText

ActiveText is to text, as ActiveRecord is to the database. ActiveText is able to parse through text reading and setting variables.

Why would you want to do this?

Ramon Tayag was looking for a way to edit SASS files in terms of variables. He wanted end-users who didn’t know anything about SASS to be able to edit variables to change colors, images, etc, in the stylesheet. To achieve that, he needed the ability to parse through text files and easy replace variables in them.

How does it work?

Given this text:

// @name Background image
// @kind file
// @description Background image. Tiled left to right, up to down, so the file has to be repeatable.
$bg_image: "http://someurl.com/bg.jpg"

// @name Link Color
// @kind color
// @description Color of the link text
$link_color: #555;

You can pass that into a ActiveText:


@at = ActiveText::Base.new(text_you_see_above)
@at[:bg_image][:name] # "Background image"
@at[:bg_image][:kind] # "file"
@at[:bg_image][:value] # %Q("http://someurl.com/bg.jpg") - note that there are double quotes in the string
@at.bg_image # same as @at[:bg_image][:value]
@at[:link_color][:kind] # "color"
@at[:link_color][:value] # "#555"
@at.link_color # same as @at[:link_color][:value]

You can also update the values:


@at = ActiveText::Base.new(text_you_see_above)
@at.bg_image = "'/path/to/image.png'"
@at.render # returns the text passed into base, with values replaced:

// @name Background image
// @kind file
// @description Background image. Tiled left to right, up to down, so the file has to be repeatable.
$bg_image: '/path/to/image.png'

// @name Link Color
// @kind color
// @description Color of the link text
$link_color: #555;

You can also mass update:

@at.update_attributes(:bg_image => %Q(“http://anotherurl.com/img.jpg”), :link_color => “green”)

Methods

.attributes

@at.attributes
returns a hash
{"mbc" => value_of_mbc, “mbc2” => value_of_mbc2}

Gotchas

Right now, only the following metadata will work:

  • name
  • kind
  • color

And only the following format to pickup values for variables will work:

$variable_name: this is the value;

Contributing to active_text

  • Check out the latest master to make sure the feature hasn’t been implemented or the bug hasn’t been fixed yet
  • Check out the issue tracker to make sure someone already hasn’t requested it and/or contributed it
  • Fork the project
  • Start a feature/bugfix branch
  • Commit and push until you are happy with your contribution
  • Make sure to add tests for it. This is important so I don’t break it in a future version unintentionally.
  • Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.

Copyright

Copyright © 2011 Ramon Tayag. See LICENSE.txt for further details.