Commit f83b8e44 authored by Rene Saarsoo's avatar Rene Saarsoo
Browse files

Created class for writing HTML source files.

Each created file will contain a <pre> element which in turn
contains each line embedded in <span id="line-n"> so that we
can later reference any desired line from documentation.

Unlike ext-doc it also escapes HTML inside JS source.
parent 58f11a5a
Loading
Loading
Loading
Loading
+9 −4
Original line number Diff line number Diff line
require 'rubygems'
require 'jsduck/aggregator'
require 'jsduck/source_formatter'
require 'jsduck/class'
require 'jsduck/tree'
require 'jsduck/tree_icons'
@@ -26,8 +27,8 @@ module JsDuck

    # Call this after input parameters set
    def run
      classes = filter_classes(parse_files(@input_files))
      copy_template(@template_dir, @output_dir)
      classes = filter_classes(parse_files(@input_files))
      write_tree(@output_dir+"/output/tree.js", classes)
      write_pages(@output_dir+"/output", classes)
    end
@@ -36,9 +37,12 @@ module JsDuck
    # documented items in all of those files.
    def parse_files(filenames)
      agr = Aggregator.new
      filenames.each do |name|
        puts "Parsing #{name} ..." if @verbose
        agr.parse(IO.read(name))
      src = SourceFormatter.new(@output_dir + "/source")
      filenames.each do |fname|
        puts "Parsing #{fname} ..." if @verbose
        code = IO.read(fname)
        agr.parse(code)
        src.write(code, fname)
      end
      agr.result
    end
@@ -83,6 +87,7 @@ module JsDuck
      end
      FileUtils.cp_r(template_dir, dir)
      FileUtils.mkdir(dir + "/output")
      FileUtils.mkdir(dir + "/source")
    end
  end

+54 −0
Original line number Diff line number Diff line
require "cgi"

module JsDuck

  # Formats JavaScript source into HTML page.  Inside the HTML every
  # source code line will be marked with ID, so that it can be linked
  # from documentation.
  class SourceFormatter

    # Initializes SourceFormatter to the directory where
    # HTML-formatted source files will be placed
    def initialize(output_dir)
      @output_dir = output_dir
    end

    # Converts source to HTML and writes into file in output
    # directory.
    def write(source, filename)
      File.open(html_filename(filename), 'w') {|f| f.write(format(source)) }
    end

    def html_filename(filename)
      @output_dir + "/" + File.basename(filename, ".js") + ".html"
    end

    # Returns full source for HTML page
    def format(source)
      return <<-EOHTML
<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>The source code</title>
</head>
<body>
  <pre>#{format_pre(source)}</pre>
</body>
</html>
      EOHTML
    end

    # Formats the contents of <pre>, wrapping each source code line
    # inside <span>.
    def format_pre(source)
      i = 0
      return source.lines.collect do |line|
        i += 1
        "<span id='line-#{i}'>#{CGI.escapeHTML(line)}</span>"
      end.join()
    end

  end

end
+37 −0
Original line number Diff line number Diff line
require "jsduck/source_formatter"

describe JsDuck::SourceFormatter do

  before do
    @formatter = JsDuck::SourceFormatter.new("some/dir")
  end

  describe "#format_pre" do

    it "adds line numbers" do
      out = @formatter.format_pre("foo\nbar\nbaz")
      out.should =~ /line-1.*foo/
      out.should =~ /line-2.*bar/
      out.should =~ /line-3.*baz/
    end

    it "line is closed inside <span>" do
      out = @formatter.format_pre("blah")
      out.should =~ /<span id='line-1'>blah<\/span>/
    end

    it "escapes HTML" do
      out = @formatter.format_pre("foo <br> bar &")
      out.should =~ /foo &lt;br&gt; bar &amp;/
    end

  end

  describe "#html_filename" do

    it "Returns filename with .html extension in output directory" do
      @formatter.html_filename("foo.js").should == "some/dir/foo.html"
    end

  end
end