Last active
January 5, 2021 04:45
-
-
Save ttscoff/8458044 to your computer and use it in GitHub Desktop.
Generate an index file for Marked from a file list using Marked's include syntax
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/ruby | |
$output = "" | |
$max_depth = 3 | |
parent_dir = false | |
if RUBY_VERSION.to_f > 1.9 | |
Encoding.default_external = Encoding::UTF_8 | |
Encoding.default_internal = Encoding::UTF_8 | |
end | |
class String | |
def file_type(f) | |
%x{mdls -raw -name kMDItemContentTypeTree "#{f}"} | |
end | |
def text_file? | |
txt_extensions = %w(.md .txt .mmd .mdown .markdown) | |
file_type(self) =~ /public\.plain-text/ || txt_extensions.include?(File.extname(self)) | |
end | |
def other? | |
rtf_extensions = %w(.pdf .opml .rtf .doc .docx .otf .odt .rtfd .app) | |
file_type(self) =~ /(com\.apple\.package|\.word\.doc)/ || rtf_extensions.include?(File.extname(self)) | |
end | |
def image? | |
img_extensions = %w(.jpg .jpeg .gif .svg .png) | |
file_type(self) =~ /public\.image/ || img_extensions.include?(File.extname(self)) | |
end | |
def code? | |
code_extensions = %w(.haml .php .m .c .h .cc .applescript .js .css .py .rb .sh .bash .zsh .xml .sass .scss .coffee .pl .yaml .plist) | |
file_type(self) =~ /public\.source-code/ || code_extensions.include?(File.extname(self)) | |
end | |
def file? | |
File.exists?(self) && File.file?(self) | |
end | |
def dir? | |
File.exists?(self) && File.directory?(self) | |
end | |
end | |
def process_file(f, parent_dir, depth) | |
return if depth > $max_depth || !File.exists?(f) | |
if f.file? && f.code? | |
file = f.strip.sub(/^#{parent_dir}\//,'') | |
$output += "<<(#{file})\n\n" | |
elsif f.file? && f.text_file? | |
# file = File.basename(f) | |
file = f.strip.sub(/^#{parent_dir}\//,'') | |
$output += "<<[#{file}]\n\n" | |
# elsif f.file? && !f.other? | |
# file = f.strip.sub(/^#{parent_dir}\//,'') | |
# $output += "<<{#{file}}\n\n" | |
elsif f.dir? && !f.other? | |
depth += 1 | |
Dir.glob(File.join(f,'*')).each {|m| | |
process_file(m, parent_dir, depth) | |
} | |
end | |
end | |
def show_usage | |
script = File.basename(__FILE__) | |
puts "Usage: #{script} file1.md file2.md [file3]" | |
puts " ls -1 | #{script}" | |
Process.exit | |
end | |
if ARGV[0] =~ /^\-?\-h(elp)?$/ | |
show_usage | |
elsif STDIN.stat.size > 0 | |
if RUBY_VERSION.to_f > 1.9 | |
input = STDIN.read.force_encoding('utf-8') | |
else | |
input = STDIN.read | |
end | |
elsif ARGV.length > 0 | |
if ARGV.length == 1 | |
file = File.expand_path(ARGV[0]) | |
if file.file? && file.text_file? | |
parent_dir = File.dirname(File.expand_path(file)) | |
Dir.chdir(parent_dir) | |
if RUBY_VERSION.to_f > 1.9 | |
input = IO.read(file).force_encoding('utf-8') | |
else | |
input = IO.read(file) | |
end | |
elsif file.dir? | |
input = file | |
parent_dir = file | |
else | |
puts "File doesn't exist: #{file}" | |
end | |
else | |
input = ARGV.join("\n") | |
end | |
else | |
puts "Invalid input" | |
show_usage | |
end | |
input.each_line do |f| | |
f.strip! | |
next if f.nil? | |
f.sub!(/\/*$/,'') | |
parent_dir = File.dirname(File.expand_path(f)) unless parent_dir | |
process_file(f, parent_dir, 0) | |
end | |
if parent_dir | |
target_name = "index" | |
target = File.join(parent_dir,"index.md") | |
while File.exists?(target) | |
target_name += "_0" if target_name == "index" | |
target_name.next! | |
target = File.join(parent_dir,"#{target_name}.md") | |
end | |
File.open(target, "w+") do |f| | |
f.puts $output | |
end | |
print target | |
else | |
Process.exit 1 | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment