Class: Stylish::Comment
- Inherits:
-
Object
- Object
- Stylish::Comment
- Includes:
- Tree::Leaf
- Defined in:
- lib/stylish/core.rb
Overview
Comment objects form the other concrete leaves of selector trees, and allow stylesheets to be annotated at any point (outside Rules). Their serialisation format follows the well-known JavaDoc and PHPDoc style, with a header, several lines of notes, and key-value information.
This format is not amendable; those desirous of their own formatting would be better served by creating their own Comment class, such as the following more basic one:
module Stylish
module Extensions
class Comment
include Tree::Leaf
attr_accessor :content
def initialize(content)
@content = content
end
def to_s(scope = "")
"/* " + content.to_s + " */"
end
end
end
end
Instance Attribute Summary collapse
-
#header ⇒ Object
readonly
Returns the value of attribute header.
-
#lines ⇒ Object
readonly
Returns the value of attribute lines.
-
#metadata ⇒ Object
readonly
Returns the value of attribute metadata.
Instance Method Summary collapse
-
#initialize(*args) ⇒ Comment
constructor
Each Comment can have a header, additional lines of text content (each provided as its own argument), and key-value metadata passed in as a Ruby Hash object.
-
#to_s(symbols = {}, scope = "") ⇒ Object
As Comment objects are the leaves of selector trees, they must implement the serialisation API of those trees, and thus the #to_s method has a scope argument, which is in practice discarded when the serialisation of the comment occurs.
Methods included from Tree::Leaf
Methods included from Tree::Node
Constructor Details
#initialize(*args) ⇒ Comment
Each Comment can have a header, additional lines of text content (each provided as its own argument), and key-value metadata passed in as a Ruby Hash object.
comment = Comment.new("My wonderful comment",
"It has several lines of insightful notes,",
"filled with wisdom and the knowledge of ages.",
{:author => "Some Egotist"})
comment.to_s # => /**
# * My wonderful comment
# *
# * It has several lines of insightful notes,
# * filled with wisdom and the knowledge of ages.
# *
# * @author Some Egotist
# */
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/stylish/core.rb', line 151 def initialize(*args) @lines, @metadata = [], {} args.each do |arg| if arg.is_a? String unless @header.nil? @lines << arg else @header = arg end elsif arg.is_a? Hash @metadata.merge! arg end end end |
Instance Attribute Details
#header ⇒ Object (readonly)
Returns the value of attribute header.
131 132 133 |
# File 'lib/stylish/core.rb', line 131 def header @header end |
#lines ⇒ Object (readonly)
Returns the value of attribute lines.
131 132 133 |
# File 'lib/stylish/core.rb', line 131 def lines @lines end |
#metadata ⇒ Object (readonly)
Returns the value of attribute metadata.
131 132 133 |
# File 'lib/stylish/core.rb', line 131 def @metadata end |
Instance Method Details
#to_s(symbols = {}, scope = "") ⇒ Object
As Comment objects are the leaves of selector trees, they must implement the serialisation API of those trees, and thus the #to_s method has a scope argument, which is in practice discarded when the serialisation of the comment occurs.
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/stylish/core.rb', line 171 def to_s(symbols = {}, scope = "") if @lines.empty? && @metadata.empty? sprintf("/**\n * %s\n */", @header) else header = sprintf(" * %s", @header) unless @header.nil? lines = @lines.map {|l| ' * ' + l }.join("\n") unless @lines.empty? = @metadata.to_a.map {|name, value| sprintf(" * @%s %s", name.to_s, value.to_s) }.join("\n") unless @metadata.empty? sprintf("/**\n%s\n */", [ header || nil, lines || nil, || nil ].compact.join("\n *\n")) end end |