class OverpassDoc::Query

Constants

ANNOTATIONS
PATTERN

Attributes

package[R]
path[R]
query[R]
raw_query[R]

Public Class Methods

new(path, query, package={}) click to toggle source
# File lib/overpass-doc/query.rb, line 28
def initialize(path, query, package={})
  ANNOTATIONS.each do |var, config|
    if config[:multi]
      instance_variable_set( "@#{var}", [] )
    else
      instance_variable_set( "@#{var}", "" )
    end
  end
  @path = path
  @query = query
  @raw_query = query
  @title = @path
  @description = ""

  ["author", "tag"].each do |annotation|
    if package[annotation]
      instance_variable_set( "@#{annotation}", package[annotation])
    end
  end
  parseQuery()
end

Public Instance Methods

description(html=false) click to toggle source
# File lib/overpass-doc/query.rb, line 54
def description(html=false)
  if html
    renderer = Redcarpet::Render::HTML.new({})
    markdown = Redcarpet::Markdown.new(renderer, {})
    return markdown.render(@description)
  end
  @description
end
extract_annotation(line) click to toggle source
# File lib/overpass-doc/query.rb, line 67
def extract_annotation(line)
  matches = line.lstrip.match(/^@([a-zA-Z]+) *(.+)$/i)
  return nil unless matches
  return matches[1], matches[2]
end
output_filename() click to toggle source
# File lib/overpass-doc/query.rb, line 50
def output_filename
  return "#{path.gsub(".op", "")}.html"
end
query_string() click to toggle source
# File lib/overpass-doc/query.rb, line 63
def query_string
  CGI::escape( @query )
end

Private Instance Methods

parseQuery() click to toggle source
# File lib/overpass-doc/query.rb, line 75
def parseQuery
  match = @raw_query.match(PATTERN)
  return if match.nil?
  if match[:error]
    raise "Invalid query"
  end


  query_lines = []
  header = true
  description = false
  description_lines = []

  match[:multi_content].split("\n").each do |line|
    if ( header )
      annotation, value = extract_annotation(line)
      if ( annotation )
        config = ANNOTATIONS[ annotation.intern ]
        if config
          if config[:multi]
            val = instance_variable_get("@#{annotation}")
            val << value.strip
          else
            instance_variable_set("@#{annotation}", value.strip)
          end
          description = true
        else
          $stderr.puts("Ignoring unknown annotation: @#{annotation}")
        end
      else
        if (description == false)
          description_lines << line.lstrip
        end
      end
    else
      header = false
      query_lines << line
    end
  end
  @description = description_lines.join("\n") unless description_lines.empty?

end