Class: QED::Document

Inherits:
Object show all
Defined in:
lib/qedoc/document.rb,
lib/qedoc/document/markup.rb

Overview

Document

TODO: css and javascripts have fixed location need to make more flexible. TODO: Have option to run documents through the runner and color code output; need htmlreporter.

Defined Under Namespace

Classes: Markup

Constant Summary collapse

DEFAULT_TITLE =
"Demonstration"
DEFAULT_CSS =

“../assets/styles/spec.css”

nil
DEFAULT_OUTPUT =
"doc/qedoc"
DEFAULT_PATH =
"test/demos"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Document

New Spec Document object.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/qedoc/document.rb', line 29

def initialize(options={})
  options.each do |k,v|
    __send__("#{k}=", v)
  end

  @title  ||= DEFAULT_TITLE
  @css    ||= DEFAULT_CSS
  @output ||= DEFAULT_OUTPUT
  @paths  ||= []

  if @paths.empty?
    dir = Dir['{test/demos,demos,demo}'].first || DEFAULT_PATH
    @paths  = File.join(dir, '**', '*')
  end
end

Instance Attribute Details

#cssObject

Returns the value of attribute css.



20
21
22
# File 'lib/qedoc/document.rb', line 20

def css
  @css
end

#dryrunObject

Returns the value of attribute dryrun.



22
23
24
# File 'lib/qedoc/document.rb', line 22

def dryrun
  @dryrun
end

#outputObject

Ouput file.



26
27
28
# File 'lib/qedoc/document.rb', line 26

def output
  @output
end

#pathsObject

Returns the value of attribute paths.



21
22
23
# File 'lib/qedoc/document.rb', line 21

def paths
  @paths
end

#quietObject

Returns the value of attribute quiet.



23
24
25
# File 'lib/qedoc/document.rb', line 23

def quiet
  @quiet
end

#titleObject

Returns the value of attribute title.



19
20
21
# File 'lib/qedoc/document.rb', line 19

def title
  @title
end

Instance Method Details

#copy_support_filesObject



124
125
126
127
128
129
130
# File 'lib/qedoc/document.rb', line 124

def copy_support_files
  make_output_directory
  %w{jquery.js}.each do |fname|
    file = File.join(File.dirname(__FILE__), 'document', fname)
    FileUtils.cp(file, output)
  end
end

#demo_filesObject

Demo files.



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/qedoc/document.rb', line 46

def demo_files
  @demo_files ||= (
    glob = paths.map do |f|
      File.directory?(f) ? Dir[File.join(f,'**/*')] : Dir[f]
    end
    glob = glob.flatten.select do |f|
      File.file?(f) && f !~ /fixtures\/|helpers\// && f !~ /\.rb$/
    end
    glob.sort
  )
end

#generateObject

Generate specification document.



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
113
114
115
116
117
118
119
120
121
# File 'lib/qedoc/document.rb', line 62

def generate
  copy_support_files

  output = ''
  files  = []

  #paths.each do |path|
  #  files.concat(Dir.glob(path).select{ |f| File.file?(f) })
  #end
  #files.sort!

  #TODO: load .config/qedrc.rb

  demo_files.each do |file|
    puts file unless quiet?

    #strio = StringIO.new('')
    #reporter = Reporter::Html.new(strio)
    #runner = Runner.new([file], reporter)
    #runner.check
    #iotext = strio.string
    #strio.close

    ext = File.extname(file)
    txt = File.read(file)

    if ext == '.qed'
      ext = file_type(txt)
    end

    #text = Tilt.new(file).render
    #html = Nokogiri::HTML(text)
    #body = html.css("body")

    text = ""
    case ext
    #when '.qed'
    #  require_qedoc
    #  markup = Markup.new(File.read(file))
    #  text << markup.to_html
    when '.rd', '.rdoc'
      require_rdoc
      require_qedoc
      markup = Markup.new(txt)
      text << markup.to_html
      #text << markup.convert(iotext, formatter)
    when '.md', '.markdown'
      require_rdiscount
      markdown = RDiscount.new(txt)
      text << markdown.to_html
    end

    output << "#{text}\n"
  end

  temp = Template.new(template, output, title, css)
  html = temp.parse_template

  save(html)
end

#make_output_directoryObject



152
153
154
# File 'lib/qedoc/document.rb', line 152

def make_output_directory
  FileUtils.mkdir_p(output)
end

#quiet?Boolean

Supress output.

Returns:

  • (Boolean)


59
# File 'lib/qedoc/document.rb', line 59

def quiet? ; @quiet ; end

#save(text) ⇒ Object

Save specification document.



141
142
143
144
145
146
147
148
149
150
# File 'lib/qedoc/document.rb', line 141

def save(text)
  if dryrun
    puts "\nwrite #{output}"
  else
    make_output_directory
    File.open(output + '/index.html', 'wb') do |f|
      f << text
    end
  end
end

#templateObject

Load specification HTML template.



133
134
135
136
137
138
# File 'lib/qedoc/document.rb', line 133

def template
  @template ||= (
    file = File.join(File.dirname(__FILE__), 'document', 'template.rhtml')
    File.read(file)
  )
end