File: _extract_examples

package info (click to toggle)
ruby-dbus 0.16.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid
  • size: 520 kB
  • sloc: ruby: 3,786; sh: 53; makefile: 8
file content (37 lines) | stat: -rwxr-xr-x 752 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#!/usr/bin/env ruby
if ARGV[0].nil?
  puts "Usage: #{$PROGRAM_NAME} file.md"
  exit
end

File.open(ARGV[0]) do |f|
  title = nil
  setup = ""
  example = ""
  f.each_line do |line|
    case line
    when /^#+ *(.*)/
      new_title = Regexp.last_match(1)

      # write previous example
      unless example.empty?
        basename = title.downcase.gsub(/ +/, "_")
        if basename == "setting_up"
          setup = example
        else
          File.open("#{basename}.rb", "w") do |e|
            e.write setup
            e.write example
            e.chmod(0o755)
          end
        end
      end

      # set new
      title = new_title
      example = ""
    when /^    (.*)/
      example << Regexp.last_match(1) << "\n"
    end
  end
end