Guest User

Creating A Ruby Static Blog

a guest
May 27th, 2014
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 14.15 KB | None | 0 0
  1. require 'redcarpet/compat'
  2. require 'yaml'
  3. require 'date'
  4.  
  5. def yaml_load filename
  6.   yaml_string = File.read filename
  7.  
  8.   YAML::load yaml_string
  9. end
  10.  
  11. def get_quote
  12.   quotes = [
  13.     ["Self-education is, I firmly believe, the only kind of education there is.", "Isaac Asimov"],
  14.     ["Any fool can know. The point is to understand.", "Albert Einstein"],
  15.     ["Live as if you were to die tomorrow. Learn as if you were to live forever.", "Mahatma Gandhi"],
  16.     ["Tell me and I forget, teach me and I may remember, involve me and I learn.", "Benjamin Franklin"],
  17.     ["[Kids] don't remember what you try to teach them. They remember what you are.", "Jim Henson"],
  18.     ["We are all failures- at least the best of us are.", "J.M. Barrie"]
  19.   ]
  20.   len = quotes.length
  21.   quote = rand(len)
  22.   return quotes[quote]
  23. end
  24.  
  25. def header (title = "I neglected to enter a title.", description = "I neglected to enter a description", quote = ["Self-education is, I firmly believe, the only kind of education there is.", "Isaac Asimov"])
  26.   x = <<-LONGHTML
  27. <!DOCTYPE html>
  28. <html lang="en">
  29.   <head>
  30.     <meta charset="utf-8">
  31.     <meta http-equiv="X-UA-Compatible" content="IE=edge">
  32.     <meta name="viewport" content="width=device-width, initial-scale=1">
  33.     <meta http-equiv="Cache-control" content="public">
  34.     <title>#{title} by Rick Peyton</title>
  35.     <meta name="description" content="#{description}">
  36.  
  37.     <!-- Bootstrap -->
  38.     <link href="//netdna.bootstrapcdn.com/bootswatch/3.1.1/flatly/bootstrap.min.css" rel="stylesheet">
  39.  
  40.     <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
  41.     <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
  42.    <!--[if lt IE 9]>
  43.      <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
  44.      <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
  45.    <![endif]-->
  46.  </head>
  47.  <body>
  48.    <div class="container">
  49.      <div class="page-header text-center">
  50.        <h4><ul class="list-inline text-center">
  51.          <li>A site about <a href="/technology">technology</a>, <a href="/ruby">ruby programming</a>, <a href="/food">food</a> &amp; <a href="/other-things">other things</a> I enjoy</li>
  52.          <li>|</li>
  53.          <li>by <a href="/about.html">Rick Peyton</a></li>
  54.        </ul></h4>
  55.        <h5>#{quote[0]} <small>― #{quote[1]}</small></h5>
  56.      </div>
  57.      <div class="row">
  58.        <div class="col-md-12 text-center">
  59.          <ul class="list-inline text-center">
  60.            <li><a href="/">Home</a></li>
  61.            <li><a href="/about.html">About</a></li>
  62.            <li><a href="/contactme.html">Contact</a></li>
  63.          </ul>
  64.        </div>
  65.      </div>
  66. LONGHTML
  67.  return x
  68. end
  69.  
  70. def post_wrap_up
  71.  x = <<-LONGHTML
  72.      <div class="row">
  73.        <div class="col-md-6">
  74.          <br>
  75.          <div class="media">
  76.            <a class="pull-left" href="/about.html">
  77.              <img class="media-object" src="/images/rick-thumb.jpg" alt="About Rick Peyton Thumbnail">
  78.            </a>
  79.            <div class="media-body">
  80.              <h4 class="media-heading">About Rick Peyton</h4>
  81.                The quick 411 on me. God, my wife, my son, my family, my golden retriever, technology, ruby programming, the green bay packers -- these are the things I love. <a href="/about.html">&#126; More about Rick Peyton</a>
  82.                <br><br>
  83.                <a href="https://twitter.com/rickpeyton" class="twitter-follow-button" data-show-count="true" data-lang="en">Follow @rickpeyton</a>
  84.            </div>
  85.          </div>
  86.          <br>
  87.        </div>
  88.        <div class="col-md-6">
  89.        </div>
  90.      </div>
  91. LONGHTML
  92.  return x
  93. end
  94.  
  95. def footer
  96.  now = DateTime.now
  97.  year = now.year
  98.  x = <<-LONGHTML
  99.      <div class="row">
  100.        <div class="col-md-12 text-center">
  101.          <hr>
  102.          <ul class="list-inline text-center">
  103.            <li><a href="/">Home</a></li>
  104.            <li><a href="/about.html">About</a></li>
  105.            <li><a href="/contactme.html">Contact</a></li>
  106.            <li>|</li>
  107.            <li>Copyright &#169; #{year} Rick Peyton</li>
  108.        </ul>
  109.        </div>
  110.      </div>
  111.    </div>
  112.    <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
  113.     <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
  114.     <!-- Include all compiled plugins (below), or include individual files as needed -->
  115.     <script src="/js/bootstrap.min.js"></script>
  116.     <!-- Google Analytics -->
  117.     <script type="text/javascript">
  118.  
  119.       var _gaq = _gaq || [];
  120.       _gaq.push(['_setAccount', 'UA-20415963-1']);
  121.       _gaq.push(['_trackPageview']);
  122.  
  123.       (function() {
  124.         var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
  125.         ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
  126.         var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  127.       })();
  128.  
  129.     </script>
  130.     <!-- Twitter Follow Widget -->
  131.     <script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src="//platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");</script>
  132.   </body>
  133. </html>
  134. LONGHTML
  135.   return x
  136. end
  137.  
  138. # List post titles on index
  139. all_index_posts = []
  140. File.open('index.html', 'w') do |file|
  141.   file.puts header "All the other Rick Peyton's are just imitating.", "My name is Rick Peyton and this is my site about technology, ruby programming, food & other things I enjoy", get_quote
  142.   Dir.chdir("./posts") do
  143.     Dir.glob("*.yml") do |name|
  144.       contents = yaml_load name
  145.       all_index_posts.push [contents[:date], contents[:title], contents[:url], contents[:category], contents[:author], contents[:post], contents[:description]]
  146.     end
  147.     # all_index_posts = [title, category, url, date]
  148.     # Sort posts in descending date order
  149.     all_index_posts = all_index_posts.sort_by{ |x| x[0] }.reverse
  150.     all_index_posts.each do |post|
  151.       pdate = post[0]
  152.       ptitle = post[1]
  153.       purl = post[2]
  154.       pcategory = post[3]
  155.       pauthor = post[4]
  156.       ppost = post[5]
  157.       pdescription = post[6]
  158.       ppost = Markdown.new(ppost).to_html
  159.       file.puts <<-LONGHTML
  160.             <div class="row">
  161.               <div class="col-md-12">
  162.                 <h1><a href="/#{pcategory}/#{purl}">#{ptitle}</a></h1>
  163.                 <p>Originally posted #{pdate}<br>
  164.                 by #{pauthor}</p>
  165.                 #{ppost}
  166.               </div>
  167.             </div>
  168.             <div class="row">
  169.               <div class="col-md-12">
  170.                 <hr>
  171.               </div>
  172.             </div>
  173. LONGHTML
  174.     end
  175.     file.puts "The End. (I haven't worked on pagination yet...)"
  176.     file.puts footer
  177.   end
  178. end
  179.  
  180. # Create category indexes
  181. # Go to the posts directory
  182. all_category_index_posts = []
  183. Dir.chdir("./posts") do
  184.   Dir.glob("*.yml") do |name|
  185.     contents = yaml_load name
  186.     all_category_index_posts.push [contents[:date], contents[:title], contents[:url], contents[:category], contents[:author], contents[:post]]
  187.   end
  188. end
  189.  
  190. # Create an empty array to hold all of the redirects
  191. redirects = []
  192. # Create individual posts
  193. Dir.chdir("./posts") do
  194.   Dir.glob("*.yml") do |name|
  195.     contents = yaml_load name
  196.     title = contents[:title]
  197.     description = contents[:description]
  198.     url = contents[:url]
  199.     category = contents[:category]
  200.     if contents[:redirect]
  201.       contents[:redirect].each do |old_path|
  202.         redirects.push "#{old_path} /#{category}/#{url}"
  203.       end
  204.     end
  205.     author = contents[:author]
  206.     date = contents[:date]
  207.     post = contents[:post]
  208.     post = Markdown.new(post).to_html
  209.     if Dir.exist?("../#{category}") == false
  210.       Dir.mkdir("../#{category}")
  211.     end
  212.     File.open("../#{category}/#{url}", 'w') do |file|
  213.       file.puts header title, description, get_quote
  214.       file.puts <<-LONGHTML
  215.             <div class="row">
  216.               <div class="col-md-12">
  217.                 <h1>#{title}</h1>
  218.                 <p>Originally posted #{date}<br>
  219.                 by #{author}</p>
  220.                 #{post}
  221.               </div>
  222.             </div>
  223.       LONGHTML
  224.       file.puts post_wrap_up
  225.       file.puts footer
  226.     end
  227.   end
  228. end
  229.  
  230. # Loop through all posts and if the category is unque add it to an array
  231. categories = []
  232. all_category_index_posts.each do |cat|
  233.   unless categories.include? cat[3]
  234.     categories.push cat[3]
  235.   end
  236. end
  237. # Take array of categories and do a new file for each
  238. # Loop through all posts and write post to page where category is equal to category in array
  239. categories.each do |category|
  240.   File.open("./#{category}/index.html", 'w') do |file|
  241.     file.puts header "All Posts Labeled #{category.capitalize}", "All Posts Labeled #{category.capitalize}", get_quote
  242.     file.puts <<-LONGHTML
  243.       <div class="row">
  244.         <div class="col-md-12"><h3>All Posts Labeled #{category.capitalize}</h3></div>
  245.       </div>
  246. LONGHTML
  247.     all_category_index_posts = all_category_index_posts.sort_by{ |x| x[0] }.reverse
  248.         all_category_index_posts.each do |post|
  249.           if post[3] == category
  250.             pdate = post[0]
  251.             ptitle = post[1]
  252.             purl = post[2]
  253.             pcategory = post[3]
  254.             pauthor = post[4]
  255.             ppost = post[5]
  256.             pdescription = post[6]
  257.             ppost = Markdown.new(ppost).to_html
  258.             file.puts <<-LONGHTML
  259.                   <div class="row">
  260.                     <div class="col-md-12">
  261.                       <h1><a href="/#{pcategory}/#{purl}">#{ptitle}</a></h1>
  262.                       <p>Originally posted #{pdate}<br>
  263.                       by #{pauthor}</p>
  264.                       #{ppost}
  265.                     </div>
  266.                   </div>
  267.                   <div class="row">
  268.                     <div class="col-md-12">
  269.                       <hr>
  270.                     </div>
  271.                   </div>
  272. LONGHTML
  273.           end
  274.         end
  275.     file.puts footer
  276.   end
  277. end
  278.  
  279. # Create an about page
  280. File.open('about.html', 'w') do |file|
  281.   now = DateTime.now
  282.   me = Date.new(1980, 11, 9)
  283.   married = Date.new(2006, 6, 24)
  284.   age = (now - me) / 365
  285.   anniversary = (now - married) / 365
  286.   abe = Date.new(2013, 11, 22)
  287.   if (now - abe) / 365 < 2
  288.     x = (now - abe) / (365 / 12)
  289.     abe_age = "#{x.to_i} month"
  290.   else
  291.     x = (now - abe) / 365
  292.     abe_age = "#{x.to_i} year"
  293.   end
  294.  
  295.   file.puts header "About Rick Peyton", "A quick 411 about me, Rick Peyton. God, my wife, my son, my family, my golden retriever, technology, ruby programming, the green bay packers -- these are the things I love.", get_quote
  296.   file.puts <<-LONGHTML
  297.       <div class="row">
  298.         <div class="col-md-12">
  299.           <img class="center-block" src="/images/rick-rebecca-and-abe.jpg">
  300.         </div>
  301.       </div>
  302.       <div class="row">
  303.         <div class="col-md-12">
  304.           <br>
  305.           <p>The quick 411 on me. God, my wife, my son, my family, my golden retriever, technology, ruby programming, the green bay packers -- these are the things I love.</p>
  306.           <p>I am #{age.to_i} years old and have been married to my beautiful wife Rebecca for #{anniversary.to_i} years. We live near Birmingham, Alabama and have a #{abe_age} old son named Abraham, a golden retriever named Hitch and a cat named Starr.</p>
  307.           <p>I have been working for a manufacturer of Christmas Photo Cards and social stationery since 2006. Officially I am listed as Operations Manager but, as with nearly any family-owned small business, my list of duties includes; web development, email marketer, in-house tech support, SEO and internet marketing project manager, garbage man or whatever else needs to be done.</p>
  308.           <p>This website is hosted through <a href = "http://www.linode.com/?r=0ed5f7143f8d066a7aef3f8cbb4c420cdb86625c">Linode</a> (2GB plan) and setup with Ubuntu 12.04.4 LTS. I purchased this domain through GoDaddy on December 13, 2010</p>
  309.           <p>This static HTML website is generated using my own custom <a href="https://www.ruby-lang.org/en/">Ruby</a> script with each post written in <a href="http://daringfireball.net/projects/markdown/">Markdown</a> and regenerated nightly.</p>
  310.         </div>
  311.       </div>
  312. LONGHTML
  313.     file.puts footer
  314. end
  315.  
  316. # Create a contact page
  317. File.open('contactme.html', 'w') do |file|
  318.   file.puts header "Get in touch with Rick Peyton", "Connect with me on Twitter or send me an email.", get_quote
  319.   file.puts <<-LONGHTML
  320.       <div class="row">
  321.         <div class="col-md-12">
  322.           <img class="center-block" src="/images/rick-rebecca-and-abe.jpg">
  323.         </div>
  324.       </div>
  325.       <div class="row">
  326.         <div class="col-md-12 text-center">
  327.           <br>
  328.           <p>I plan to add a respectable contact form in the near future.</p>
  329.           <p>But for now just send me a message on <a href="https://twitter.com/rickpeyton">twitter</a> or email me therickpeyton@gmail.com</p>
  330.         </div>
  331.       </div>
  332. LONGHTML
  333.     file.puts footer
  334. end
  335.  
  336. # Process any 301 redirects
  337. File.open(".htaccess", 'w') do |file|
  338.   file.puts <<-LONGHTML
  339.     <IfModule mod_expires.c>
  340.       # Enable expirations
  341.       ExpiresActive On
  342.       # Default directive
  343.       ExpiresDefault "access plus 1 year"
  344.       # My favicon
  345.       ExpiresByType image/x-icon "access plus 1 year"
  346.       # Images
  347.       ExpiresByType image/gif "access plus 1 year"
  348.       ExpiresByType image/png "access plus 1 year"
  349.       ExpiresByType image/jpg "access plus 1 year"
  350.       ExpiresByType image/jpeg "access plus 1 year"
  351.       # CSS
  352.       ExpiresByType text/css "access plus 1 year"
  353.       # Javascript
  354.       ExpiresByType application/javascript "access plus 1 year"
  355.       Header set Cache-Control "max-age=31536000, public"
  356.     </IfModule>
  357.     RewriteEngine On
  358.       RewriteCond %{HTTPS} !=on
  359.       RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
  360. LONGHTML
  361.   file.puts "Redirect 301 /googleplus https://plus.google.com/u/0/102108553145967911282"
  362.   file.puts "Redirect 301 /about /about.html"
  363.   redirects.each do |redirect|
  364.     file.puts "Redirect 301 #{redirect}"
  365.   end
  366. end
Advertisement
Add Comment
Please, Sign In to add comment