Initial Revision
[ohcount] / lib / ohcount / parser.rb
1 class Ohcount::Parser
2         # Parses a file, and store the results in files on disk.
3         #
4         # This method primarily exists for the benefit of the Ohloh analysis engine,
5         # which prefers to store interim results in files on disk. It is unlikely to be
6         # useful to the general public.
7         #
8         # A subdirectory is created for each language found in the target file.
9         # Within each language subdirectory, separate files are created for code, comments, and blanks.
10         #
11         # The 'code' file contains code lines, the 'comments' file contains comment lines, and the 'blanks'
12         # file contains a count of the number of blank lines in the file.
13         def self.parse_to_dir(args)
14                 arg_keys = [
15                         :dir,       # directory to parse to
16                         :buffer,    # buffer contents of the what we're parsing
17                         :polyglot   # the polyglot name of what we're parsing
18                 ]
19                 raise ArgumentError.new('Missing required args') unless (arg_keys - args.keys).empty?
20
21                 polyglot = args[:polyglot].to_s
22                 dir = args[:dir]
23                 buffer = args[:buffer].to_s
24
25                 language_breakdowns = Ohcount::parse(buffer, polyglot)
26                 language_breakdowns.each do |lb|
27                         lb_dest_dir = dir + "/" + lb.name
28                         Dir.mkdir(lb_dest_dir)
29                         if (lb.code)
30                                 File.open(lb_dest_dir + "/code", "w") do |io|
31                                         code = lb.code
32                                         io.write code
33                                         io.write "\n" unless (code.size == 0 || code[-1,1] == "\n")
34                                 end
35                         end
36                         if (lb.comment)
37                                 File.open(lb_dest_dir + "/comment", "w") do  |io|
38                                         comment = lb.comment
39                                         io.write comment
40                                         io.write "\n" unless (comment.size == 0 || comment[-1,1] == "\n")
41                                 end
42                         end
43                         File.open(lb_dest_dir + "/blanks", "w") do |io|
44                                 io.write lb.blanks.to_s
45                         end
46                 end
47         end
48 end