Class: AwsMust::Docu

Inherits:
Object
  • Object
show all
Includes:
Utils::MyLogger
Defined in:
lib/aws-must/docu.rb

Constant Summary collapse

PROGNAME =

progname for logger

"docu"
DEFAULT_OPEN_TAG =

constants

"+++start+++"
DEFAULT_CLOSE_TAG =
"+++close+++"
DEFAULT_FOLD_ON_TAG =
"+++fold-on+++"
DEFAULT_FOLD_OFF_TAG =
"+++fold-off+++"
DEFAULT_INCLUDE_TAG =
">"

Constants included from Utils::MyLogger

Utils::MyLogger::LOGFILE

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils::MyLogger

#getLogger

Constructor Details

#initialize(template, options = {}) ⇒ Docu


Constructor

  • template: which knows how to return template files



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/aws-must/docu.rb', line 36

def initialize( template, options={} )

  @logger = getLogger( PROGNAME, options )
  @logger.info( "#{__method__}, options=#{options}" )

  @template = template

  # command line option define lines to output for directive
  @fold_on = options[:fold_on] if options[:fold_on]
  @fold_off = options[:fold_off] if options[:fold_off]


  @directives = {
    :open => DEFAULT_OPEN_TAG,
    :close => DEFAULT_CLOSE_TAG,
    :fold_on => DEFAULT_FOLD_ON_TAG,
    :fold_off => DEFAULT_FOLD_OFF_TAG,
    :include => DEFAULT_INCLUDE_TAG,
  }


end

Instance Attribute Details

#fold_offObject

Returns the value of attribute fold_off.



30
31
32
# File 'lib/aws-must/docu.rb', line 30

def fold_off
  @fold_off
end

#fold_onObject

command line option define lines to output for directive



29
30
31
# File 'lib/aws-must/docu.rb', line 29

def fold_on
  @fold_on
end

Instance Method Details

#directive_close(line) ⇒ Object

return bool, line_pre, line



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/aws-must/docu.rb', line 146

def directive_close( line )
  #return line && line.include?( get_tag( :close ) )
  if line && line.include?( get_tag( :close ) ) then
    # index = line.index( get_tag(:close) )
    # return true, index == 0 ? nil : line[0..index-1],  line[ index+ get_tag(:close).length..-1]
    # do not output anythin on line for directive
    return true, nil, nil
    
  elsif line && line.include?( get_tag(:fold_off) ) then
    # replace fold-off direcive line with fold_off text
    return true, self.fold_off, nil
  else
    return false, nil, line
  end
end

#directive_include(line) ⇒ Object

return include ‘template’ to include if it is include directive



164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/aws-must/docu.rb', line 164

def directive_include( line )

  # not valid input
  return nil unless line
  
  # extract template_name using regexp
  parse_regexp=/^#{get_tag(:include)}\s+(?<template>[\w]*)\s*/
  parsed = line.match( parse_regexp )

  # not found
  return nil unless parsed
  return parsed['template']

end

#directive_open(line) ⇒ Object


Check if line satisfies tag



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/aws-must/docu.rb', line 125

def directive_open( line )
  # return line && line.include?( get_tag(:open) )
  if line && line.include?( get_tag(:open) ) then
    # find index pointing _after_ :open tag
    # index = line.index( get_tag(:open) ) + get_tag(:open).length
    # # return text after directtive
    # return  true, line[ index..-1]

    # do not output anything for the line
    return  true, nil

  elsif line && line.include?( get_tag(:fold_on) ) then
    # output fold_on text instread
    return  true, self.fold_on
  else
    return false, nil
  end
end

#document(template_name) ⇒ Object


docu - output documentation to stdout



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/aws-must/docu.rb', line 62

def document( template_name )

  @logger.info( "#{__method__}, template_name '#{template_name}'" )
  
  # 
  template_string = @template.get_template( template_name )
  @logger.debug( "#{__method__}, template_string= '#{template_string}'" )

  # 
  state_opened=false

  # iterate lines
  template_string && template_string.split( "\n" ).each do | line |

    if !state_opened then

      # waiting for +++open+++ or +++fold_on+++

      open_found, line = directive_open( line )
      if open_found then

        state_opened = true
        @logger.info( "#{__method__}, open tag in '#{template_name}'" )
        
        # something follows the start -tag
        redo if line && ! line.empty?

      end

    else 

      # seen +++open+++ or +++fold_on+++, waiting for +++close+++, +++fold_off+++

      close_found, line_pre, line = directive_close( line )
      # puts "close_found=#{close_found}, #{close_found.class}, line=#{line}"
      if  close_found then
        output( line_pre ) if line_pre && !line_pre.empty?
        @logger.info( "#{__method__}, close tag in '#{template_name}'" )
        state_opened = false
        redo if line && ! line.empty?
      elsif template_name = directive_include( line ) then
        @logger.info( "#{__method__}, include in '#{template_name}'" )
        document( template_name )
      else
        output( line )
      end

    end # state_opened
  end # each line

end

#get_tag(tag) ⇒ Object


get tag value



182
183
184
185
# File 'lib/aws-must/docu.rb', line 182

def get_tag( tag )
  raise "Unknown tag" unless @directives[tag]
  return @directives[tag]
end

#output(line) ⇒ Object


output - document output



117
118
119
# File 'lib/aws-must/docu.rb', line 117

def output( line )
  puts( line ) 
end