Class: IndyDoc
- Inherits:
-
Object
- Object
- IndyDoc
- Defined in:
- lib/indydoc.rb
Instance Attribute Summary collapse
-
#to_s ⇒ Object
readonly
Returns the value of attribute to_s.
Instance Method Summary collapse
-
#initialize(file = nil, path: File.dirname(__FILE__)) ⇒ IndyDoc
constructor
A new instance of IndyDoc.
- #merge(rbfile = nil) ⇒ Object
- #parse(filepath) ⇒ Object
- #save(filepath = @file) ⇒ Object
- #to_md ⇒ Object
- #to_px ⇒ Object
Constructor Details
#initialize(file = nil, path: File.dirname(__FILE__)) ⇒ IndyDoc
Returns a new instance of IndyDoc.
15 16 17 18 19 |
# File 'lib/indydoc.rb', line 15 def initialize(file=nil, path: File.dirname(__FILE__)) @file = file @px = Polyrex.new file if file @path = path end |
Instance Attribute Details
#to_s ⇒ Object (readonly)
Returns the value of attribute to_s.
13 14 15 |
# File 'lib/indydoc.rb', line 13 def to_s @to_s end |
Instance Method Details
#merge(rbfile = nil) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/indydoc.rb', line 21 def merge(rbfile = nil) buffer = File.read rbfile @px.records.each do |x| x.records.each do |method| if method.desc.strip.length > 0 then buffer.sub!(/( *)def\s+#{method.name}/) do |y| indent = ($1) comment = if method.desc.lines.length < 2 then method.desc.lines\ .map {|line| "%s# %s\n" % [indent,line.chomp]} else lines = method.desc.lines\ .map {|line| "%s# %s" % [indent,line.chomp]} [indent + '#'] << lines << [indent + "#\n"] end y.sub(/(?= *def\s+#{method.name})/, comment.join("\n")) end end end end @to_s = buffer end |
#parse(filepath) ⇒ Object
59 60 61 62 |
# File 'lib/indydoc.rb', line 59 def parse(filepath) mp = MethodParser.new File.read(filepath) @raw_xml = mp.to_xml end |
#save(filepath = @file) ⇒ Object
64 65 66 67 68 69 70 |
# File 'lib/indydoc.rb', line 64 def save(filepath=@file) raise 'IndyDoc#save: please supply a file path' if filepath.nil? @px ? px.save(filepath) : File.write(filepath, @px=self.to_px) puts 'saved' end |
#to_md ⇒ Object
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 |
# File 'lib/indydoc.rb', line 72 def to_md() doc = Rexle.new(@raw_xml) lines = doc.root.elements.map do |klass| classname = "# %s\n\n" % klass.attributes[:name] methods = klass.elements.map do |meth| mname = "## %s\n" % meth.attributes[:name] mscope = "scope: %s\n" % meth.attributes[:scope] margs = "args: \n" args = meth.attributes[:args].split(/, */)\ .map {|x| " %s # # " % x}.join("\n") + "\n" desc = "desc: \n\n" [mname, mscope, margs, args, desc].join("\n") end.join("\n") classname + methods end lines.join("\n") end |
#to_px ⇒ Object
102 103 104 105 106 107 108 109 |
# File 'lib/indydoc.rb', line 102 def to_px doc = Nokogiri::XML(@raw_xml) xsl_source = File.read(File.join(@path,'to_polyrex.xsl')) #xsl_source = open(File.join(@path,'template.xsl'), # 'UserAgent' => 'IndyDoc').read xslt = Nokogiri::XSLT(xsl_source) xslt.transform(doc).to_s end |