Skip to content

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

@DouglasAllen

Description

@DouglasAllen

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions