-
Notifications
You must be signed in to change notification settings - Fork 0
/
fabric-entry-creation.html
48 lines (44 loc) · 9.36 KB
/
fabric-entry-creation.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<!DOCTYPE html>
<html> <head lang=en><meta charset=UTF-8><title>Fabric Entry Creation | EF</title><link href=//cdnjs.cloudflare.com/ajax/libs/font-awesome/4.2.0/css/font-awesome.min.css rel=stylesheet><link href=http://fonts.googleapis.com/css?family=Inconsolata rel=stylesheet type=text/css><link rel=stylesheet href=http://nafiulis.me/theme/css/main.css><link rel=stylesheet href=http://nafiulis.me/theme/css/pygment.css><script src=http://nafiulis.me/theme/js/jquery.min.js></script><script src=http://nafiulis.me/theme/js/main.js></script></head> <body> <!--Heading at the top saying "Engineering Fantasy"--> <div id=header_top> <div class=title> <a href=http://nafiulis.me><span id=engineering>Engineering</span><span id=fantasy>Fantasy</span></a> </div> </div> <button type=button class="js-menu-trigger sliding-menu-button button-nav"> <img src=https://raw.githubusercontent.com/thoughtbot/refills/master/source/images/menu-white.png alt="Menu Icon"> </button> <!--Navigation Bar--> <nav class="js-menu sliding-menu-content"> <span class=section-header>Pages</span> <ul> <li><a href=http://nafiulis.me>Home</a></li> <li><a href=http://nafiulis.me/tags.html>Tags</a></li> <li><a href=http://nafiulis.me/pages/about-me.html>About Me</a></li> </ul> <span class=section-header>Categories</span> <ul> <li><a href=http://nafiulis.me/category/anime.html>Anime</a></li> <li><a href=http://nafiulis.me/category/education.html>Education</a></li> <li><a href=http://nafiulis.me/category/productivity.html>Productivity</a></li> <li><a href=http://nafiulis.me/category/programming.html>programming</a></li> <li><a href=http://nafiulis.me/category/rants.html>rants</a></li> </ul> </nav> <div class="js-menu-screen menu-screen"></div> <!--Main Container--> <div class=container> <!--Top most menu--> <div id=menu> <div class=left> <a href=http://nafiulis.me/feeds/all.atom.xml><i class="fa fa-rss
icon"></i></a> <a href=https://twitter.com/gamesbrainiac><i class="fa fa-twitter icon"></i></a> </div> <div class=center> <h1 class=message>Nafiul Islam on casting spells with code</h1> </div> <div class=right> <a href=https://github.com/gamesbrainiac><i class="fa fa-github icon"></i></a> <a href=http://stackoverflow.com/users/1624921/games-brainiac><i class="fa fa-stack-overflow icon" style="padding-right: 30px"></i></a> </div> </div> <!--Main blog list container--> <div id=blogroll> <div class=article-container> <h1>Fabric Entry Creation</h1> <p class=time>Wednesday, 11 December 2013</p> <div class=article-content> <p>So, in my attempts to create a functioning static blog generator using pelican, I've been using fabric to create my entries so that I can easily sort them according to their dates.</p> <p>Here's the code:</p> <div class=highlight><pre><span></span><span class=k>def</span> <span class=nf>make_entry</span><span class=p>(</span><span class=n>title</span><span class=o>=</span><span class=s1>'default'</span><span class=p>):</span>
<span class=n>today</span> <span class=o>=</span> <span class=n>datetime</span><span class=o>.</span><span class=n>datetime</span><span class=o>.</span><span class=n>today</span><span class=p>()</span>
<span class=n>slug</span> <span class=o>=</span> <span class=n>title</span><span class=o>.</span><span class=n>lower</span><span class=p>()</span><span class=o>.</span><span class=n>strip</span><span class=p>()</span><span class=o>.</span><span class=n>replace</span><span class=p>(</span><span class=s1>' '</span><span class=p>,</span> <span class=s1>'-'</span><span class=p>)</span>
<span class=n>f_create</span> <span class=o>=</span> <span class=s2>"content/{}_{:0>2}_{:0>2}_{}.rst"</span><span class=o>.</span><span class=n>format</span><span class=p>(</span><span class=n>today</span><span class=o>.</span><span class=n>year</span><span class=p>,</span> <span class=n>today</span><span class=o>.</span><span class=n>month</span><span class=p>,</span> <span class=n>today</span><span class=o>.</span><span class=n>day</span><span class=p>,</span> <span class=n>slug</span><span class=p>)</span>
<span class=k>with</span> <span class=nb>open</span><span class=p>(</span><span class=n>f_create</span><span class=p>,</span> <span class=s1>'w'</span><span class=p>)</span> <span class=k>as</span> <span class=n>w</span><span class=p>:</span>
<span class=k>with</span> <span class=nb>open</span><span class=p>(</span><span class=s1>'template.txt'</span><span class=p>,</span> <span class=s1>'r'</span><span class=p>)</span> <span class=k>as</span> <span class=n>r</span><span class=p>:</span>
<span class=n>s</span> <span class=o>=</span> <span class=n>r</span><span class=o>.</span><span class=n>read</span><span class=p>()</span><span class=o>.</span><span class=n>format</span><span class=p>(</span><span class=n>title</span><span class=o>=</span><span class=n>title</span><span class=p>,</span>
<span class=n>hashes</span><span class=o>=</span><span class=s1>'#'</span> <span class=o>*</span> <span class=nb>len</span><span class=p>(</span><span class=n>title</span><span class=p>),</span>
<span class=n>year</span><span class=o>=</span><span class=n>today</span><span class=o>.</span><span class=n>year</span><span class=p>,</span>
<span class=n>month</span><span class=o>=</span><span class=n>today</span><span class=o>.</span><span class=n>month</span><span class=p>,</span>
<span class=n>day</span><span class=o>=</span><span class=n>today</span><span class=o>.</span><span class=n>day</span><span class=p>,</span>
<span class=n>hour</span><span class=o>=</span><span class=n>today</span><span class=o>.</span><span class=n>hour</span><span class=p>,</span>
<span class=n>minute</span><span class=o>=</span><span class=n>today</span><span class=o>.</span><span class=n>minute</span><span class=p>,</span>
<span class=n>slug</span><span class=o>=</span><span class=n>slug</span><span class=p>)</span>
<span class=n>w</span><span class=o>.</span><span class=n>write</span><span class=p>(</span><span class=n>s</span><span class=p>)</span>
<span class=k>print</span><span class=p>(</span><span class=s2>"File created -> "</span> <span class=o>+</span> <span class=n>f_create</span><span class=p>)</span>
</pre></div> <p>Now, when I was doing this, I initally used the <tt class="docutils literal">datetime.date</tt> class, but that did not have the <tt class="docutils literal">hour</tt> and the <tt class="docutils literal">minute</tt> attributes in the class. I also did not want to bother with <tt class="docutils literal">slug</tt> creation, so I got that build in too.</p> <p>I tried to get all the template string inside the function, but later on decided to refactor it out to a <tt class="docutils literal">template.txt</tt>, which I can more easily use and change:</p> <div class=highlight><pre><span></span>{title}
{hashes}
:date: {year}-{month}-{day} {hour}:{minute}
:tags:
:category:
:slug: {slug}
:author: Nafiul Islam
#Post goes here
</pre></div> <p>The great thing about fabric is how easy it makes writing command line arguments. So, if I wanted to create a file with say <tt class="docutils literal">{year}_{month}_{day}_{title}</tt>, I'd just do this:</p> <div class=highlight><pre><span></span>fab make_entry:'My awesome title here'
</pre></div> <p>Now that <em>is</em> awesome! :D</p> </div> </div> <div class=post-meta><span class=meta-type>Category: </span> <span><a href=http://nafiulis.me/category/programming.html>Programming</a></span> <span class=meta-type> Tags: </span> <span> <a href=http://nafiulis.me/tag/firsts.html>firsts</a>, <a href=http://nafiulis.me/tag/post.html>post</a>, <a href=http://nafiulis.me/tag/fabric.html>fabric</a>, <a href=http://nafiulis.me/tag/python.html>python</a> </span> </div> <div id=disqus_thread style="margin-top: 10px; margin-left: 20px; margin-right: 20px;"></div> <script type=text/javascript>
/* * * CONFIGURATION VARIABLES: EDIT BEFORE PASTING INTO YOUR WEBPAGE * * */
var disqus_shortname = 'nafiulisme'; // required: replace example with your forum shortname
/* * * DON'T EDIT BELOW THIS LINE * * */
(function() {
var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
})();
</script> <noscript>Please enable JavaScript to view the <a href=http://disqus.com/?ref_noscript>comments powered by Disqus.</a></noscript> </div> <!--Footer--> <div id=footer> <footer> Code examples licenced under MIT License <br> Copyright <i class="fa fa-copyright"></i> 2018 Quazi Nafiul Islam </footer> </div> </div> <script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-55554110-1', 'auto');
ga('send', 'pageview');
</script> </body> </html>