chishgg-slate 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md ADDED
@@ -0,0 +1,33 @@
1
+ # Slate
2
+
3
+ A Markdown to HTML generator written in Ruby while participating in DUMMIES by Hack Club.
4
+
5
+ ## Installation
6
+
7
+ TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
8
+
9
+ Install the gem and add to the application's Gemfile by executing:
10
+
11
+ ```bash
12
+ bundle add UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
13
+ ```
14
+
15
+ If bundler is not being used to manage dependencies, install the gem by executing:
16
+
17
+ ```bash
18
+ gem install UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
19
+ ```
20
+
21
+ ## Usage
22
+
23
+ TODO: Write usage instructions here
24
+
25
+ ## Development
26
+
27
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
28
+
29
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
30
+
31
+ ## Contributing
32
+
33
+ Bug reports and pull requests are welcome on GitHub at https://github.com/chishxd/slate.
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "standard/rake"
5
+
6
+ task default: :standard
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Slate
4
+ VERSION = "0.1.0"
5
+ end
data/lib/slate.rb ADDED
@@ -0,0 +1,95 @@
1
+ require_relative 'slate/version'
2
+ require 'kramdown'
3
+ require 'kramdown-parser-gfm'
4
+ require 'optparse'
5
+
6
+ module Slate
7
+ class Error < StandardError; end
8
+
9
+ Options = Struct.new(:output_dir)
10
+ # The main class to rule them all
11
+ class CLI
12
+ # This method is the MAIN method... Refactored from bin/slate
13
+ def self.run(argv)
14
+ options = Options.new(nil)
15
+
16
+ OptionParser.new do |opts|
17
+ opts.banner = 'Usage: slate [options] <file_or_directory>'
18
+
19
+ opts.on('-o', '--output DIR', 'Specify the output direc1tory') do |dir|
20
+ options.output_dir = dir
21
+ end
22
+
23
+ opts.on('-v', '--version', 'Show current version') do |_ver|
24
+ puts Slate::VERSION
25
+ exit
26
+ end
27
+
28
+ opts.on('-h', '--help', 'View Help') do
29
+ puts opts
30
+ exit
31
+ end
32
+ end.parse!(argv)
33
+
34
+ path = argv.first
35
+ if path.nil?
36
+ puts 'Error: You must provide a file or directory.'
37
+ puts opt_parser
38
+ exit
39
+ end
40
+
41
+ path = File.expand_path(path)
42
+ if File.file?(path)
43
+ process_file(path, options)
44
+ elsif File.directory?(path)
45
+ process_directory(path, options)
46
+ else
47
+ puts "Error: #{path} Is neither a file, nor a directory"
48
+ end
49
+ end
50
+
51
+ def self.convert(md_content, title)
52
+ raw_html = Kramdown::Document.new(md_content, input: 'GFM').to_html
53
+
54
+ "<head>\n<title> #{title} </title>\n <link href='style.css' rel='stylesheet'>\n</head>\n<body class='markdown-body'>\n#{raw_html}\n</body>"
55
+ end
56
+
57
+ def self.copy_css(dest)
58
+ FileUtils.mkdir_p(dest)
59
+ style_path = File.join(__dir__, '..', 'style.css')
60
+ FileUtils.cp(style_path, dest)
61
+ end
62
+
63
+ def self.save_file(path, options)
64
+ content = File.read(path)
65
+ filename = File.basename(path, '.*')
66
+ dir = options.output_dir || File.dirname(path)
67
+
68
+ FileUtils.mkdir_p(dir)
69
+ output_path = File.join(dir, "#{filename}.html")
70
+ final_html = convert(content, filename)
71
+ File.write(output_path, final_html)
72
+ puts "Saved #{filename}.html"
73
+ end
74
+
75
+ def self.process_file(path, options)
76
+ puts 'Argument seems to be a single file, Parsing it'
77
+ dest = options.output_dir || File.dirname(path)
78
+ copy_css(dest)
79
+ save_file(path, options)
80
+ end
81
+
82
+ def self.process_directory(path, options)
83
+ puts 'Found a directory, gotta check all .md files!'
84
+ dir = options.output_dir || path
85
+ copy_css(dir)
86
+
87
+ files = Dir.glob(File.join(path, '*.md'))
88
+
89
+ files.each do |file|
90
+ save_file(file, options)
91
+ end
92
+ puts "Done! Processed #{files.count} files."
93
+ end
94
+ end
95
+ end
data/sig/slate.rbs ADDED
@@ -0,0 +1,4 @@
1
+ module Slate
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end