JoshCheek / ruby-kickstart

An interactive guide to learning the Ruby programming language.
MIT License
375 stars 353 forks source link

Still working on the mardown stuff Josh but I have something. #17

Open DouglasAllen opened 9 years ago

DouglasAllen commented 9 years ago

K! First of what you can do as what I am doing, go get practicing ruby articles here on github. Practicing Ruby journal. http://github.com/elm-city-craftworks/practicing-ruby-manuscripts That has the code I'm using to do this with.

Next is your option. In Rakefile

filecontent = Kramdown::Document.new(filecontent, :coderay_line_numbers => nil, template: "#{__dir__}/templates/default.html.erb")

I chose to not include line numbers. Your choice though. If you want line numbers then just remove the nil hash. My Rackefile now is:

require "kramdown"
require "coderay"
require "fileutils"

MARKDOWN_FILES = Dir.glob("#{__dir__}/articles/**/*.md")

task default: :html_files

desc "Generate HTML files from markdown articles"
task :html_files do
  MARKDOWN_FILES.each do |markdown_file|
    html_path = markdown_file.sub("/articles/", "/articles-html/").sub(/\.md$/, ".html")
    puts "Generating #{html_path}"
    FileUtils.mkdir_p(File.dirname(html_path))
    File.open(html_path, "w") do |html_file|
      filecontent = File.read(markdown_file)
      filecontent = filecontent.gsub("\`\`\`", "~~~")
      filecontent = Kramdown::Document.new(filecontent, template: "#{__dir__}/templates/default.html.erb")
      html_file.write(filecontent.to_html)
    end
  end
end

desc "Delete all generated HTML files"
task :clean do
  FileUtils.rm_rf("#{__dir__}/articles-html")
end

ALL_FILES = Dir.glob("#{__dir__}/articles/**/*")
desc "make md files from ruby files"
task :make_and_add_ruby_tags_to_files do
  ALL_FILES.each do |file|
    if !File.directory?(file)
      File.open(file, "r") do |f|
        content = File.read(f)        
        content = "```ruby\n" + content + "```"
        path = f.path
        markdown_file_name = path << ".md"      
        File.open(markdown_file_name, "w") {|f| f.write(content) }          
      end
      File.delete(file)
      p file  
    end        
  end            
end

It's not 100% because of image files but you only have one right? I deleted it before generating any html files. http://myrackapps.herokuapp.com/htdocs/ruby-kickstart-master/session6/notes/09-static-files-and-bundler/public/dependencies.png But added it back in after.

So you see we need some further tasks to do that so there won't be one manual manipulation like this and for future uses.

I would suggest you give a commented name to all these files so that they show up in the converted files. Maybe you know better than I how we could just take the Rakefile and do it. We have the file names after all. But I'm just glade I don't have to do this manually now. What a relief. ;-)

I've really learned a lot from guys like you, Gregory, github folks in general and all the other ones that I know from the web.

DouglasAllen commented 9 years ago

May I have your permission to share this last link with folks I know like on Facebook and Google+? I am in and I manage a few groups on these. I don't know how long I will keep it up there but you may get more downloads from your repo or followers on your Vimeo. ;-) Is your site on Heroku a Sinatra or Rails or is it some other stack?

JoshCheek commented 9 years ago

All the material here is open source, you can share it.