Computer Science Mojo

~ David's Notes on coding, software and computer science




Post

My pelican setup

Category: Web     Tag: Pelican   Python  
By: David     On: Sat 09 July 2016     

Document some of the useful changes that I made to my pelican setup. Might be helpful for newcomers ;)

Reading

Useful Pelican commands with make

  • make serve
  • make html
  • make publish
  • make newpost NAME="post name"
  • make newpage NAME="page name"

Linking favicon.ico robots.txt

See Tips — Pelican 3.6.3 documentation

Clear content and Github hosting

Setting DELETE_OUTPUT_DIRECTORY = True helps clean the folder of any old unused posts or images, but if the output folder is a git repo, add ".git" to OUTPUT_RETENTION

DELETE_OUTPUT_DIRECTORY = True
OUTPUT_RETENTION = [".git","CNAME","README.md","favicon.ico","robots.txt"]

Makfile

Slight modifications from Tips n Tricks:

  • Seperated post and page directory
  • Removed edit post / page
  • Added an if statement to check if file exits and abort if it exists
# my add stuff #######
POSTSDIR=$(INPUTDIR)/posts
PAGESDIR=$(INPUTDIR)/pages
DATE := $(shell date +'%Y-%m-%d %H:%M:%S')
SLUG := $(shell echo '${NAME}' | sed -e 's/[^[:alnum:]]/-/g' | tr -s '-' | tr A-Z a-z)
EXT := md
EDITOR = medit 

newpost:
ifeq ($(wildcard $(POSTSDIR)/$(SLUG).$(EXT)),) 
else
    @echo 'File exists'
    exit 1;
endif
ifdef NAME
    echo "Title: $(NAME)" >  $(POSTSDIR)/$(SLUG).$(EXT)
    echo "Slug: $(SLUG)" >> $(POSTSDIR)/$(SLUG).$(EXT)
    echo "Status: published" >> $(POSTSDIR)/$(SLUG).$(EXT)
    echo "Date: $(DATE)" >> $(POSTSDIR)/$(SLUG).$(EXT)
    echo "Category: " >> $(POSTSDIR)/$(SLUG).$(EXT)
    echo "Tags: " >> $(POSTSDIR)/$(SLUG).$(EXT)
    echo "Author: David" >> $(POSTSDIR)/$(SLUG).$(EXT)
    echo ""              >> $(POSTSDIR)/$(SLUG).$(EXT)
    echo ""              >> $(POSTSDIR)/$(SLUG).$(EXT)
    ${EDITOR} ${POSTSDIR}/${SLUG}.${EXT} &
else
    @echo 'Variable NAME is not defined.'
    @echo 'Do make newpost NAME='"'"'Post Name'"'"
endif


newpage:
ifeq ($(wildcard $(PAGESDIR)/$(SLUG).$(EXT)),) 
else
    @echo 'File exists'
    exit 1;
endif
ifdef NAME
    echo "Title: $(NAME)" >  $(PAGESDIR)/$(SLUG).$(EXT)
    echo "Slug: $(SLUG)" >> $(PAGESDIR)/$(SLUG).$(EXT)
    echo "Status: published" >> $(PAGESDIR)/$(SLUG).$(EXT)
    echo "Date: $(DATE)" >> $(PAGESDIR)/$(SLUG).$(EXT)
    echo "Tags: " >> $(PAGESDIR)/$(SLUG).$(EXT)
    echo "Author: David" >> $(PAGESDIR)/$(SLUG).$(EXT)
    echo ""              >> $(PAGESDIR)/$(SLUG).$(EXT)
    echo ""              >> $(PAGESDIR)/$(SLUG).$(EXT)
    ${EDITOR} ${PAGESDIR}/${SLUG}.$(EXT)
else
    @echo 'Variable NAME is not defined.'
    @echo 'Do make newpage NAME='"'"'Page Name'"'"
endif

TEMPLATE_PAGES

For the comment page and the 404 page, I created them based on the base template and wanted them to be copied over to output

TEMPLATE_PAGES = {'inc/comments.html': 'pages/comments.html', 'inc/404.html': '404.html'}

Plugins

  • Left the plugins that don't effect the preview out of pelicanconf.py. This increased make html speed. In my case 'yuicompressor' and 'ga_page_view'

  • Created a GOOGLE_ANALYTICS_BOOL to be only enabled in publishconf.py

{% if GOOGLE_ANALYTICS_BOOL %}
    <script type="text/javascript">
        <!-- tracking code -->
    </script>
{% endif %}
  • for pelicanconf.py
PLUGINS = ['summary', 'better_figures_and_images', 'pelican_javascript']
SUMMARY_USE_FIRST_PARAGRAPH = True
RESPONSIVE_IMAGES = True
GOOGLE_ANALYTICS_BOOL = False # True in publish
  • for publishconf.py
PLUGINS.append('yuicompressor')
PLUGINS.append('ga_page_view')
GOOGLE_ANALYTICS_BOOL = True

ga_page_view Plugin

First and foremost - Great plugin ! Some notes below:
* The plugin does the same thing as HelloAnalytics.py from Google Analytics and only reads the first view from the first property of the first account, so you should make sure this is the one you want

Traceback (most recent call last):
  File "HelloAnalytics.py", line 6, in <module>
    from oauth2client.service_account import ServiceAccountCredentials
ImportError: cannot import name ServiceAccountCredentials
pip install oauth2client==2.2.0
  • For error
ERROR: Cannot load plugin `ga_page_view`
  | ImportError: cannot import name SignedJwtAssertionCredentials
pip install oauth2client==1.5.2