foucault 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,77 @@
1
+ #!/usr/bin/env ruby
2
+ require 'ostruct'
3
+ require 'optparse'
4
+
5
+ # Set all default options
6
+ options = OpenStruct.new
7
+ options.mirror = false
8
+ options.output_dir = ''
9
+ options.output = ''
10
+ options.debug = false
11
+
12
+ OptionParser.new do |opts|
13
+
14
+ opts.banner = "Literate programming with Foucault -- taking a narrative turn"
15
+ opts.separator ""
16
+ opts.separator "Usage: foucault [options] input files"
17
+ opts.separator ""
18
+ opts.separator "Options:"
19
+
20
+ opts.on("-o",
21
+ "--output [PATH]",
22
+ "Path to output collected program text to") do |path|
23
+ options.output = path
24
+ end
25
+
26
+ opts.on("-d", "--debug", "Generate program texts in debug mode") do |d|
27
+ options.debug = true
28
+ end
29
+
30
+ opts.on_tail("-h",
31
+ "--help",
32
+ "Show this message") do
33
+ puts opts
34
+ end
35
+
36
+ end.parse!
37
+
38
+
39
+
40
+ document = []
41
+ ARGV.each do |input_file|
42
+ document.concat File.readlines input_file
43
+ end
44
+ def program_collector(document, debug = false)
45
+ program = []
46
+
47
+ require 'foucault/code_block_determinator'
48
+
49
+ line_determinator = CodeBlockDeterminator.new
50
+
51
+ while not document.empty? do
52
+
53
+ line = document.shift
54
+
55
+ if line_determinator.collect_line? line
56
+ program.push line
57
+ elsif debug
58
+ program.push "\n"
59
+ else
60
+ # ignore this line
61
+ end
62
+
63
+ end
64
+
65
+ program.join
66
+ end
67
+ program = program_collector document, options.debug
68
+
69
+ if not options.output.empty? then
70
+ # try to write collected program text to file specified in options.output
71
+ File.open(options.output, "w") do |file|
72
+ file.puts program
73
+ end
74
+ else
75
+ # No output file specified: use STDOUT
76
+ puts program
77
+ end
@@ -0,0 +1,71 @@
1
+ # Copyright 2014 Huub de Beer
2
+ #
3
+ # This file is part of Foobar.
4
+ #
5
+ # Foucault is free software: you can redistribute it and/or modify it under
6
+ # the terms of the GNU General Public License as published by the Free
7
+ # Software Foundation, either version 3 of the License, or (at your option)
8
+ # any later version.
9
+ #
10
+ # Foucault is distributed in the hope that it will be useful, but WITHOUT ANY
11
+ # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12
+ # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
13
+ # details.
14
+ #
15
+ # You should have received a copy of the GNU General Public License along with
16
+ # Foucault. If not, see <http://www.gnu.org/licenses/>.
17
+ class CodeBlockDeterminator
18
+
19
+ def initialize()
20
+ to_start_state
21
+ end
22
+
23
+ def collect_line?(line)
24
+
25
+ case @state
26
+
27
+ when :fenced_block
28
+ if is_fence?(line) and fence_size(line) >= @size and fence_type(line) == @type then
29
+ # recognized the end of this code block
30
+ to_start_state
31
+ else
32
+ # We're still in a code block: collect this line
33
+ return true
34
+ end
35
+
36
+ when :start
37
+ if is_fence?(line)
38
+ # Start of a new code block
39
+ @state = :fenced_block
40
+ @type = fence_type line
41
+ @size = fence_size line
42
+ end
43
+ end
44
+
45
+ # not in a code block: don't collect this line
46
+ return false
47
+ end
48
+
49
+ private
50
+
51
+ def to_start_state()
52
+ @state = :start
53
+ @size = 0
54
+ @type = :none
55
+ end
56
+
57
+ FENCE = /^(?<fence>(?<type>~|`){3,}).*$/
58
+
59
+ def is_fence?(line)
60
+ FENCE.match line
61
+ end
62
+
63
+ def fence_size(line)
64
+ FENCE.match(line)[:fence].size
65
+ end
66
+
67
+ def fence_type(line)
68
+ FENCE.match(line)[:type]
69
+ end
70
+
71
+ end
metadata ADDED
@@ -0,0 +1,49 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: foucault
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Huub de Beer
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-05-01 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: ! 'Literate programming for every language: extract code from markdown
15
+ documents'
16
+ email: H.T.de.Beer@gmail.com
17
+ executables:
18
+ - foucault
19
+ extensions: []
20
+ extra_rdoc_files: []
21
+ files:
22
+ - lib/foucault/code_block_determinator.rb
23
+ - bin/foucault
24
+ homepage: https://github.com/htdebeer/foucault
25
+ licenses:
26
+ - GPL v.3
27
+ post_install_message:
28
+ rdoc_options: []
29
+ require_paths:
30
+ - lib
31
+ required_ruby_version: !ruby/object:Gem::Requirement
32
+ none: false
33
+ requirements:
34
+ - - ! '>='
35
+ - !ruby/object:Gem::Version
36
+ version: '0'
37
+ required_rubygems_version: !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ! '>='
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ requirements: []
44
+ rubyforge_project:
45
+ rubygems_version: 1.8.23
46
+ signing_key:
47
+ specification_version: 3
48
+ summary: ! 'Taking a narrative turn to programming: literate programming'
49
+ test_files: []