Skip to content

Instantly share code, notes, and snippets.

@ShaneQful
Last active July 19, 2019 04:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ShaneQful/4506253 to your computer and use it in GitHub Desktop.
Save ShaneQful/4506253 to your computer and use it in GitHub Desktop.
Something I made to make a diary for my final year project. Markdown to html, create a table of contents for each entry, commit to the repo & scp to web directory on the server. Now with fancier table of contents.
#!/usr/bin/ruby
require "open3"
def bash input
streams = Open3.popen3 input
out = ""
while((line=streams[1].gets) != nil)
out += line
end
return out
end
file = File.open(ARGV[0], "rb")
diary = ""
file.readlines.each do |l|
diary += l
end
file.close
entry_points = diary.scan /Date\s[a-zA-Z]+\s\d+\s[a-zA-Z]+\s\d{4}/
table_of_contents = "<div style=\"position: fixed; right: 100px; top: 50px; display: block;\">\<h5>Table of Contents:</h5>\n<ol>"
entry_points.each do |e|
diary = diary.gsub(e,e+"\n<a name=\"#{e.gsub(/\s/,"")}\"></a>")
table_of_contents += "<li><a href=\"##{e.gsub(/\s/,"")}\">#{e}</a></li>\n"
end
table_of_contents += "</ol></div>"
diary['table_of_contents'] = table_of_contents
File.open('temp_diary', 'w') do |f1|
f1.puts diary
end
bash "cat temp_diary | markdown > Diary.html"
bash "rm temp_diary"
bash "scp Diary.html user@server:~/"
File.open('move_diary.sh', 'w') do |f1|
f1.puts "mv Diary.html public_html/"
f1.puts "chmod 444 public_html/Diary.html"
end
bash "cat move_diary.sh | ssh user@server bash -s"
bash "git add Diary.html Diary.md"
bash "git commit -m \"#{ARGV[1]}\""
bash "git push origin"
@ShaneQful
Copy link
Author

Probably should have done this is bash all things considered ;P

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment