Skip to content

Commit

Permalink
build blog with sphinx
Browse files Browse the repository at this point in the history
  • Loading branch information
iswbm committed Aug 4, 2020
1 parent 02cbc5e commit 44f719f
Show file tree
Hide file tree
Showing 171 changed files with 13,607 additions and 0 deletions.
35 changes: 35 additions & 0 deletions make.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
@ECHO OFF

pushd %~dp0

REM Command file for Sphinx documentation

if "%SPHINXBUILD%" == "" (
set SPHINXBUILD=sphinx-build
)
set SOURCEDIR=source
set BUILDDIR=build

if "%1" == "" goto help

%SPHINXBUILD% >NUL 2>NUL
if errorlevel 9009 (
echo.
echo.The 'sphinx-build' command was not found. Make sure you have Sphinx
echo.installed, then set the SPHINXBUILD environment variable to point
echo.to the full path of the 'sphinx-build' executable. Alternatively you
echo.may add the Sphinx directory to PATH.
echo.
echo.If you don't have Sphinx installed, grab it from
echo.http://sphinx-doc.org/
exit /b 1
)

%SPHINXBUILD% -M %1 %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O%
goto end

:help
%SPHINXBUILD% -M help %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O%

:end
popd
149 changes: 149 additions & 0 deletions md2rst.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
# coding:utf-8
import os
# import commands
import subprocess
import platform

from git import Repo


osName = platform.system()
repo_path ='.'
if (osName == 'Windows'):
repo_path = 'E:\\MING-Git\\magic-python'
blog_path = 'E:\\MING-Git\\magic-python\\source'
index_path = 'E:\\MING-Git\\magic-python\\README.md'
elif (osName == 'Darwin'):
repo_path = '/Users/MING/Github/magic-python/'
blog_path = '/Users/MING/Github/magic-python/source'
index_path = '/Users/MING/Github/magic-python/README.md'


repo = Repo.init(path=repo_path)
if not repo.is_dirty():
# 没有文件变更
os._exit(0)

base_link = "http://python.iswbm.com/en/latest/"
readme_header = '''
![](http://image.iswbm.com/20200607120940.png)
<p align="center">
<img src='https://img.shields.io/badge/language-Python-blue.svg' alt="Build Status">
<img src='https://img.shields.io/badge/framwork-Sphinx-green.svg'>
<a href='https://www.zhihu.com/people/wongbingming'><img src='https://img.shields.io/badge/dynamic/json?color=0084ff&logo=zhihu&label=%E7%8E%8B%E7%82%B3%E6%98%8E&query=%24.data.totalSubs&url=https%3A%2F%2Fapi.spencerwoo.com%2Fsubstats%2F%3Fsource%3Dzhihu%26queryKey%3Dwongbingming'></a>
<a href='https://juejin.im/user/5b08d982f265da0db3502c55'><img src='https://img.shields.io/badge/掘金-2481-blue'></a>
<a href='http://image.iswbm.com/20200607114246.png'><img src='http://img.shields.io/badge/%E5%85%AC%E4%BC%97%E5%8F%B7-30k+-brightgreen'></a>
</p>
## [项目主页](http://python.iswbm.com/)
在线阅读:[Python 编程时光](http://python.iswbm.com/)
![](http://image.iswbm.com/20200607130051.png)
## 文章结构
![](http://image.iswbm.com/20200607131339.png)
'''
readme_tooter = '''
---
![](http://image.iswbm.com/20200607174235.png)
'''


def get_file_info(filename):
with open(filename, 'r', encoding="utf-8") as file:
first_line = file.readline().replace("#", "").strip()
return first_line.split(' ', 1)

def make_line(chapter, file):
page_name, _ = os.path.splitext(file)
(index, title) = get_file_info(file)
url = base_link + chapter + "/" + page_name + ".html"
item_list = ["-", index, "[{}]({})\n".format(title, url)]
return " ".join(item_list)

def render_index_page(index_info):
'''
生成 readme.md 索引文件,包含所有文件目录
'''
# 重新排序
index_info = sorted(index_info.items(), key=lambda item:item[0], reverse=False)

# 写入文件
with open(index_path, 'w+', encoding="utf-8") as file:
file.write(readme_header)
for chp, info in index_info:
chp_name = info["name"]
file.write("## " + chp_name + "\n")
for line in info["contents"]:
file.write(line)
file.write("\n")
file.write(readme_tooter)

def convert_md5_to_rst(file):
'''
转换格式:md5转换成rst
'''
(filename, extension) = os.path.splitext(file)
convert_cmd = 'pandoc -V mainfont="SimSun" -f markdown -t rst {md_file} -o {rst_file}'.format(
md_file=filename+'.md', rst_file=filename+'.rst'
)
# status, output = commands.getstatusoutput(convert_cmd)
status = subprocess.call(convert_cmd.split(" "))
if status != 0:
print("命令执行失败: " + convert_cmd)
os._exit(1)
if status == 0:
print(file + ' 处理完成')
else:
print(file + '处理失败')

def get_all_dir():
'''
获取所有的目录
'''
dir_list = []
file_list = os.listdir(blog_path)
for item in file_list:
abs_path = os.path.join(blog_path, item)
if os.path.isdir(abs_path):
dir_list.append(abs_path)
return dir_list


def init_index_info():
'''
初始化索引
'''
index_info = {}
chapter_dir = os.path.join(blog_path, "chapters")
os.chdir(chapter_dir)
for file in os.listdir(chapter_dir):
name, _ = os.path.splitext(file)
with open(file, 'r', encoding="utf-8") as f:
chapter_name = f.readlines()[1].strip()
index_info[name.replace("p", "")] = {"name": chapter_name, "contents": []}
return index_info

def main(index_info):
for folder in get_all_dir():
os.chdir(folder)
chapter = os.path.split(folder)[1]
all_file = os.listdir(folder)
all_md_file = sorted([file for file in all_file if file.endswith('md')])

for file in all_md_file:
line = make_line(chapter, file)
index_info[chapter.replace("c", "")]["contents"].append(line)
convert_md5_to_rst(file)


if __name__ == '__main__':
index_info = init_index_info()
main(index_info)
# render_index_page(index_info)
print("OK")
7 changes: 7 additions & 0 deletions source/_static/js/baidutongji.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
var _hmt = _hmt || [];
(function() {
var hm = document.createElement("script");
hm.src = "https://hm.baidu.com/hm.js?26ee367cf320d48d046542295cdb0dad";
var s = document.getElementsByTagName("script")[0];
s.parentNode.insertBefore(hm, s);
})();
50 changes: 50 additions & 0 deletions source/_static/js/readmore.js

Large diffs are not rendered by default.

66 changes: 66 additions & 0 deletions source/c01/c01_01.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# 1.1 默默无闻的省略号很好用

![](http://image.iswbm.com/20200804124133.png)

在Python中,一切皆对象,省略号也不例外。

在 Python 3 中你可以直接写 `...` 来得到它

```python
>>> ...
Ellipsis
>>> type(...)
<class 'ellipsis'>
```

而在 Python 2 中没有`...` 这个语法,只能直接写Ellipsis来获取。

```python
>>> Ellipsis
Ellipsis
>>> type(Ellipsis)
<type 'ellipsis'>
>>>
```

它转为布尔值时为真

```python
>>> bool(...)
True
```

最后,这东西是一个单例。

```python
>>> id(...)
4362672336
>>> id(...)
4362672336
```

那这东西有啥用呢?

1. 它是 Numpy 的一个语法糖
2. 在 Python 3 中可以使用 ... 代替 pass

```shell
$ cat demo.py
def func01():
...

def func02():
pass

func01()
func02()

print("ok")

$ python3 demo.py
ok
```



![](http://image.iswbm.com/20200607174235.png)
69 changes: 69 additions & 0 deletions source/c01/c01_01.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
1.1 默默无闻的省略号很好用
==========================

|image0|

在Python中,一切皆对象,省略号也不例外。

在 Python 3 中你可以直接写 ``...`` 来得到它

.. code:: python
>>> ...
Ellipsis
>>> type(...)
<class 'ellipsis'>
而在 Python 2 中没有\ ``...`` 这个语法,只能直接写Ellipsis来获取。

.. code:: python
>>> Ellipsis
Ellipsis
>>> type(Ellipsis)
<type 'ellipsis'>
>>>
它转为布尔值时为真

.. code:: python
>>> bool(...)
True
最后,这东西是一个单例。

.. code:: python
>>> id(...)
4362672336
>>> id(...)
4362672336
那这东西有啥用呢?

1. 它是 Numpy 的一个语法糖
2. 在 Python 3 中可以使用 … 代替 pass

.. code:: shell
$ cat demo.py
def func01():
...
def func02():
pass
func01()
func02()
print("ok")
$ python3 demo.py
ok
|image1|

.. |image0| image:: http://image.iswbm.com/20200804124133.png
.. |image1| image:: http://image.iswbm.com/20200607174235.png

36 changes: 36 additions & 0 deletions source/c01/c01_02.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# 1.2 使用 end 来结束代码块
有不少编程语言,循环、判断代码块需要用 end 标明结束,这样一定程度上会使代码逻辑更加清晰一点。

但是其实在 Python 这种严格缩进的语言里并没有必要这样做。

如果你真的想用,也不是没有办法,具体你看下面这个例子。

```python
__builtins__.end = None


def my_abs(x):
if x > 0:
return x
else:
return -x
end
end

print(my_abs(10))
print(my_abs(-10))
```

执行后,输出如下

```shell
[root@localhost ~]$ python demo.py
10
10
```



![](http://image.iswbm.com/20200804124133.png)

![](http://image.iswbm.com/20200607174235.png)
Loading

0 comments on commit 44f719f

Please sign in to comment.