Skip to content

Instantly share code, notes, and snippets.

@os6sense
Created January 24, 2018 10:51
Show Gist options
  • Save os6sense/367516ede87de428befc0b6205a84674 to your computer and use it in GitHub Desktop.
Save os6sense/367516ede87de428befc0b6205a84674 to your computer and use it in GitHub Desktop.
require 'find'
""" asciidoc
Class: Directory Treee
Recurse down a directory tree
"""
module EAsciidoc
class DirectoryTree
def initialize(directories)
@directories = directories
end
def find_all_rb_files(&block)
@directories.each do |directory|
Find.find(directory) do |path|
next unless path.end_with? '.rb'
yield path if block_given?
end
end
end
end
end
#!/usr/bin/env ruby
require 'pry'
require_relative 'directory_tree'
""" asciidoc
Embedded Asciidoc section
++ 'fda'fdsa $853% alsfdkj
"""
module EAsciidoc
class Sections
def initialize
@within_section = false
@sections = []
end
def open_section
@within_section = true
@sections << Section.new
end
def close_section
@within_section = false
end
def <<(str)
return unless @within_section
@sections.last << str
end
def each(&block)
@sections.each do |s|
yield s if block_given?
end
end
end
class Section
def initialize
@text = ''
end
def <<(str)
@text += str
end
def to_s
@text
end
end
end
def parse_quoted(file_contents)
file_contents.lines.each do |line|
if line.strip == '""" asciidoc'
@asciidoc_sections.open_section
elsif line.strip == '"""'
@asciidoc_sections.close_section
else
@asciidoc_sections << line
end
end
end
directories = [ARGV[0]]
@asciidoc_sections = ::EAsciidoc::Sections.new
dt = ::EAsciidoc::DirectoryTree.new(directories)
dt.find_all_rb_files do |path|
STDERR.puts path
parse_quoted(File.read(path))
end
@asciidoc_sections.each { |s| puts s.to_s }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment