使用Python在GitHub Pages上运行博客

GitHub是一种非常流行的源代码控制Web服务,它使用Git将本地文件与GitHub服务器上保存的副本同步,以便您轻松共享和备份您的工作。

除了为代码存储库提供用户界面之外,GitHub还使用户能够直接从存储库发布网页 。 GitHub推荐的网站生成软件包是Jekyll ,用Ruby编写。 由于我是Python的忠实粉丝,因此我更喜欢Pelican ,这是一个基于Python的博客平台,可与GitHub很好地结合使用。

Pelican和Jekyll都将以MarkdownreStructuredText编写的内容转换为HTML以生成静态网站,并且两个生成器都支持允许无限定制的主题。

在本文中,我将介绍如何安装Pelican,设置GitHub存储库,运行快速入门帮助程序,编写一些Markdown文件以及发布第一页。 我假设您有一个GitHub帐户 ,熟悉基本的Git命令 ,并想使用Pelican发布博客。

安装Pelican并创建仓库

首先,必须在本地计算机上安装Pelican(和ghp-import )。 使用Python软件包安装工具pip ,这非常容易(您对pip正确吗?):

 $ pip install pelican ghp-import Markdown 

接下来,打开浏览器并在GitHub上为您的新博客创建一个新的存储库。 命名如下(在此处以及整个教程中,用您的GitHub用户名替换<username>):

 https://GitHub.com/username/username.github.io 

留空; 我们将在稍后用引人注目的博客内容来填充它。

使用命令行(您的命令行正确吗?),将空的Git存储库克隆到本地计算机:


   
   
$ git clone https: // GitHub.com / username / username.github.io blog
$ cd blog

那一个奇怪的把戏……

这是关于在GitHub上发布Web内容的一个非常明显的技巧。 对于用户页面(托管在名为username.github.io的存储库中的页面),其内容由master分支提供。

我强烈建议不要将所有Pelican配置文件和原始Markdown文件都保留在master中 ,而只保留在Web内容中。 因此,我将Pelican配置和原始内容保留在一个我喜欢称为content的单独分支中。 (您可以随意调用它,但以下说明将其称为content 。)我喜欢这种结构,因为我可以丢弃master中的所有文件,然后用content分支重新填充它。


   
   
$ git checkout -b content
Switched to a new branch 'content'

配置鹈鹕

现在该进行内容配置了。 Pelican提供了一个很棒的初始化工具,称为pelican-quickstart ,它将询问您有关博客的一系列问题。


   
   
$ pelican-quickstart
Welcome to pelican-quickstart v3.7.1.

This script will help you create a new Pelican-based website.

Please answer the following questions so this script can generate the files
needed by Pelican.

> Where do you want to create your new web site? [ . ]  
> What will be the title of this web site? Super blog
> Who will be the author of this web site? username
> What will be the default language of this web site? [ en ]
> Do you want to specify a URL prefix? e.g., http: // example.com   ( Y / n ) n
> Do you want to enable article pagination? ( Y / n )
> How many articles per page do you want? [ 10 ]
> What is your time zone? [ Europe / Paris ] US / Central
> Do you want to generate a Fabfile / Makefile to automate generation and publishing? ( Y / n ) y
> Do you want an auto-reload & simpleHTTP script to assist with theme and site development? ( Y / n ) y
> Do you want to upload your website using FTP? ( y / N ) n
> Do you want to upload your website using SSH? ( y / N ) n
> Do you want to upload your website using Dropbox? ( y / N ) n
> Do you want to upload your website using S3? ( y / N ) n
> Do you want to upload your website using Rackspace Cloud Files? ( y / N ) n
> Do you want to upload your website using GitHub Pages? ( y / N ) y
> Is this your personal page ( username.github.io ) ? ( y / N ) y
Done. Your new project is available at / Users / username / blog

您可以对每个问题采用默认值,但以下情况除外:

  • 网站标题,应该唯一且特殊
  • 网站作者,可以是个人用户名或您的全名
  • 时区,可能不在巴黎
  • 上载到GitHub Pages,在我们的案例中为“ y”

回答完所有问题后,Pelican在当前目录中保留以下内容:


   
   
$ ls
Makefile                content /        develop_server.sh *
fabfile.py              output /         pelicanconf.py
publishconf.py

您可以检查出的鹈鹕文档了解如何使用这些文件,但我们都是把事情做对现在完成了。 不,我也没有阅读文档。

继续前进

将所有Pelican生成的文件添加到本地Git存储库的内容分支,提交更改,然后通过输入以下内容将本地更改推送到GitHub上托管的远程存储库:


   
   
$ git add .
$ git commit -m 'initial pelican commit to content'
$ git push origin content

这并不是超级令人兴奋的事情,但是如果我们需要将编辑还原到这些文件之一,这将非常方便。

终于到了某个地方

好的,现在您可以博客了! 您的所有博客文章,照片,图像,PDF等都将位于内容目录中,该目录最初为空。 要开始创建第一篇文章和带有照片的“关于”页面,请输入:


   
   
$ cd content
$ mkdir pages images
$ cp / Users / username / SecretStash / HotPhotoOfMe.jpg images
$ touch first-post.md
$ touch pages / about.md

接下来,在您喜欢的文本编辑器中打开空文件first-post.md并添加以下内容:


   
   
title: First Post on My Sweet New Blog
date: <today's date>
author: Your Name Here

# I am On My Way To Internet Fame and Fortune!

This is my first post on my new blog. While not super informative it
should convey my sense of excitement and eagerness to engage with you,
the reader!

前三行包含Pelican用于组织事物的元数据。 您可以在其中放置许多不同的元数据。 同样,文档是您了解更多有关选项的最佳选择。

现在,打开空文件pages / about.md并添加以下文本:


   
   
title: About
date: <today's date>

![So Schmexy][my_sweet_photo]

Hi, I am <username> and I wrote this epic collection of Interweb
wisdom. In days of yore, much of this would have been deemed sorcery
and I would probably have been burned at the stake.

😆

[my_sweet_photo]: {static}/images/HotPhotoOfMe.jpg

现在,您的内容目录中将包含三个新的Web内容。 内容分支。 内容很多。

发布

不用担心 回报来了!

剩下要做的就是:

  • 运行Pelican以在输出中生成静态HTML文件:
     $  pelican content -o output -s publishconf.py 
    
  • 使用ghp-import输出目录的内容添加到master分支中:
     $  ghp-import -m "Generate Pelican site" --no-jekyll -b master output 
    
  • 将本地master分支推送到远程仓库:
     $  git push origin master 
    
  • 提交新内容并将其推送到内容分支:
    
         
         
    $ git add content
    $ git commit -m 'added a first post, a photo and an about page'
    $ git push origin content

天哪,我做到了!

现在,当您可以查看自己发布的内容以供所有人查看时,令人兴奋的部分就在这里! 打开浏览器并输入:

 https://username.github.io 

祝贺您在GitHub上自行发布的新博客! 每当您要添加更多页面或文章时,都可以遵循此模式。 快乐的博客。

翻译自: https://opensource.com/article/19/5/run-your-blog-github-pages-python

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值