Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test #5

Open
zhangliwen opened this issue Aug 11, 2021 · 2 comments
Open

test #5

zhangliwen opened this issue Aug 11, 2021 · 2 comments

Comments

@zhangliwen
Copy link
Owner

zhangliwen commented Aug 11, 2021

[TOC]

Python 2.x 和 Python3.x 的区别

前言

future 模块

Python 3 介绍的 一些 Python 2 不兼容的关键字和特性可以通过在 Python 2 的内置 __future__ 模块导入。如果你计划让你的代码支持 Python 3,建议你使用 __future__ 模块导入。例如,如果我想要 在Python 2 中表现 Python 3 中的整除,我们可以通过如下导入。

from __future__ import division

官方参考文档

Python 2.0 到 3.9 的全部新变化

编码

源码文件编码

在 python 2.x 版本,python 源码文件默认使用 Ascii 编码,所以无法直接显示中文。需要在源码文件头部通过声明 # -*- encoding: utf-8 -*- 来告诉解释器使用 utf-8 编码处理源文件。

而 python 3.x 源码文件默认以 UTF-8 编码方式处理。

字符串编码

早期 Unicode 标准并未发布时,python 的字符串str默认使用ASCII进行编码。后来python2.0提供了一种新的存储文本数据的数据类型:Unicode 对象,添加了对 Unicode 的支持。它可以用来存储和操作 Unicode 数据。所以 python 2 同时有Ascii编码的strUnicode 字符串。

到了 python 3 以后,python删除了Unicode 对象类型,使字符串均由Unicode编码。

标准输入输出

输出

删除了 python 2 的print语句 ,取而代之的是 print()函数。 Python 2.6 与 Python 2.7 部分地支持这种形式的 print 语法。在 Python 2.6 与Python 2.7 里面,以下三种形式是等价的:

print "fish"
print ("fish") # 注意print后面有个空格
print("fish") # print()不能带有任何其它参数

如果 Python 2.x 版本想使用使用 Python 3.x 的 print 函数,可以导入 __future__包,该包禁用 Python2.x 的 print 语句,采用 Python 3.x 的 print 函数

from __future__ import print_function
print("fish", "panda", sep=', ')

输入

在 python 2 中常用两个函数进行输入:

  1. input()

    把用户的输入按照代码进行解释,得到解释后的值,例如:

    用户输入 得到的值
    name 识别为变量name,取变量name的值,不存在则报错
    123 整数123
    3.14 浮点数3.14
    "123" 字符串类型的123
  2. raw_input()

    将所有的输入识别为字符串。

而 python 3 只有input()函数且等价于 python 2 的raw_input()函数。

运算符

除法

python 2 中除法a/b

  • 若a、b都是整数则地板除,结果向下取整(结果为负时,取小于该结果的第一个负整数),例如(-5/2=-3)。
  • 若a、b至少一个为浮点数,则结果也是相对精确的浮点数;对于a//b,无论a、b为何种数字类型结果都向下取整,只是a、b有浮点数时结果取整后以浮点数形式显示(5.0//2=2.0)。

python 3 中a/b默认结果总为浮点数,a//b与python2一致。

不等运算符

Python 2.x 中不等于有两种写法!=<>,Python 3.x 中去掉了<>, 只有!=一种写法。

八进制的表示方式

Python 2 可以用0开头或者0o开头表示八进制,python 3 只能用0o开头表示。

数据类型

删除 long 类型

python 3 删除了long类型,现在只有一种整型——int,但它的行为就像 2.x 版本的long

新增 bytes 类型

python 3 新增了二进制序列类型 bytes。bytes 对象是由单个字节构成的不可变序列,即字符经过编码的以八位二进制为单位的序列。

另外,在 Python 2.x 系列中,允许 8 位字符串( 2.x 所提供的最接近内置二进制数据类型的对象)与 Unicode 字符串进行各种隐式转换。 这是为了实现向下兼容的变通做法,以适应 Python 最初只支持 8 位文本而 Unicode 文本是后来才被加入这一事实。 在 Python 3.x 中,这些隐式转换已被取消 —— 8 位二进制数据与 Unicode 文本间的转换必须显式地进行,bytes 与字符串对象的比较结果将总是不相等。

返回迭代器

map、filter 和 reduce 等高阶函数返回值的类型由列表改为迭代器,另外 reduce 函数在 Python 3.x 中已经不属于 built-in 了,被挪到 functools 模块当中。

返回视图对象

字典的keys()values()items()方法在 python 2 中返回列表,在 python 3 中返回可迭代的视图对象。

关于类

经典类与新式类

python 2 允许创建一个经典类,即不继承任何类,包括object类。创建新式类要显式继承父类。

python 3 不再支持经典类,若定义时不显式继承任何类,则默认继承object类。

特殊函数 super

python 2 使用super()在子类方法关联父类对应方法时必须要传递父类名和子类的selfsuper()作为参数,而在 python 3 可以省略super()的参数。

class C(B):
    def method(self, arg):
        # super().父类对应方法(父类的参数)
        super().method(arg)
        # 上一句写法在Python3等价于如下语句
        # super(C, self).method(arg)

删除 repr 表达式

Python 2.x 中反引号``相当于repr函数的作用,返回一个字符串对象。

Python 3.x 中去掉了这种写法,只允许使用repr函数。

range 与 xrange

python 2.x 中range函数返回一个数字列表,而xrange函数返回 xrange 对象,它表示一个不可变的数字序列。xrange 对象相比常规 list 或 tuple 的优势在于一个 xrange 对象总是占用固定数量的(较小)内存,不论其所表示的范围有多大(因为它只保存了 start, stop 和 step 值,并会根据需要计算具体单项或子范围的值)。这一点与生成器的惰性计算有点相似,它是可迭代对象但并不是生成器或者迭代器。

python 3.x 中只保留了range函数,它与xrange十分相似,但是支持了更多功能,如 3.2 支持了切片操作。

另外,在 3.3 版更改: 定义 '==' 和 '!=' 以根据 range 对象所定义的值序列来进行比较(而不是根据对象的标识)。也就是说,如果两个 range 对象表示相同的值序列就认为它们是相等的。请注意比较结果相等的两个 range 对象可能会具有不同的 start,stop 和 step 属性,例如 range(0) == range(2, 1, 3)range(0, 3, 2) == range(0, 4, 2)

异常

在 Python 3 中处理异常也轻微的改变了,在 Python 3 中我们现在使用 as 作为关键词。

捕获异常的语法由 except exc, var 改为 except exc as var

使用语法except (exc1, exc2) as var 可以同时捕获多种类别的异常。 Python 2.6 已经支持这两种语法。

  • 在 2.x 版本,所有类型的对象都是可以被直接抛出的,在 3.x 时代,只有继承自BaseException的对象才可以被抛出。
  • 在 2.x 版本raise 语句使用逗号将抛出对象类型和参数分开,3.x 取消了这种奇葩的写法,直接调用构造函数抛出对象即可。如raise TypeError('MyRange expected 1 arguments, got 0')

布尔值

TrueFalse 在 Python 2 中是两个全局变量,既然是变量,那么他们就可以指向其它对象,例如:

# python 2.x
print True
print False
True = 123
False = 321
print True
print False
# 输出结果如下:
# True
# False
# 123
# 321

Python 3 修正了这个缺陷,True 和 False 变为两个关键字,永远指向两个固定的对象,不允许再被重新赋值。

新增 nonlocal 语句

nonlocal 语句会使得所列出的名称指向之前在最近的包含作用域中绑定的除全局变量以外的变量。例如内层函数的局部变量n绑定外层函数的局部变量n。 这种功能很重要,因为绑定的默认行为是先搜索局部命名空间。 这个语句允许被封装的代码重新绑定局部作用域以外且非全局(模块)作用域当中的变量。

回收列表推导式的变量

在 python 2中,如下列表推导式中 for 语句的变量 x 在后面的代码中可被使用:

y = [x for x in xrange(10)]
print x
print y
# 输出如下:
# 9
# [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

而 python 3,变量 x 作为局部变量在列表推导式执行结束后就被回收了,后续无法访问。

y = [x for x in range(10)]
print(x)
print(y)
# 输出:
# Traceback (most recent call last):
#   File "<input>", line 2, in <module>
# NameError: name 'x' is not defined

不可排序类型的报错

当对不可排序类型做比较的时候,python 2 会返回 False,python 3 会抛出一个类型错误:

# python 2.x
print [1, 2, 3] > 'abc'
# False

# python 3.x
print([1, 2, 3] > 'abc')
# Traceback (most recent call last):
#   File "E:/CS_Practice/python/2021/main.py", line 4, in <module>
#     print([1, 2, 3] > 'abc')
# TypeError: '>' not supported between instances of 'list' and 'str'
@zhangliwen
Copy link
Owner Author

[TOC]

Python 2.x 和 Python3.x 的区别

前言

future 模块

Python 3 介绍的 一些 Python 2 不兼容的关键字和特性可以通过在 Python 2 的内置 __future__ 模块导入。如果你计划让你的代码支持 Python 3,建议你使用 __future__ 模块导入。例如,如果我想要 在Python 2 中表现 Python 3 中的整除,我们可以通过如下导入。

from __future__ import division

官方参考文档

Python 2.0 到 3.9 的全部新变化

编码

源码文件编码

在 python 2.x 版本,python 源码文件默认使用 Ascii 编码,所以无法直接显示中文。需要在源码文件头部通过声明 # -*- encoding: utf-8 -*- 来告诉解释器使用 utf-8 编码处理源文件。

而 python 3.x 源码文件默认以 UTF-8 编码方式处理。

字符串编码

早期 Unicode 标准并未发布时,python 的字符串str默认使用ASCII进行编码。后来python2.0提供了一种新的存储文本数据的数据类型:Unicode 对象,添加了对 Unicode 的支持。它可以用来存储和操作 Unicode 数据。所以 python 2 同时有Ascii编码的strUnicode 字符串。

到了 python 3 以后,python删除了Unicode 对象类型,使字符串均由Unicode编码。

标准输入输出

输出

删除了 python 2 的print语句 ,取而代之的是 print()函数。 Python 2.6 与 Python 2.7 部分地支持这种形式的 print 语法。在 Python 2.6 与Python 2.7 里面,以下三种形式是等价的:

print "fish"
print ("fish") # 注意print后面有个空格
print("fish") # print()不能带有任何其它参数

如果 Python 2.x 版本想使用使用 Python 3.x 的 print 函数,可以导入 __future__包,该包禁用 Python2.x 的 print 语句,采用 Python 3.x 的 print 函数

from __future__ import print_function
print("fish", "panda", sep=', ')

输入

在 python 2 中常用两个函数进行输入:

  1. input()

    把用户的输入按照代码进行解释,得到解释后的值,例如:

    用户输入 得到的值
    name 识别为变量name,取变量name的值,不存在则报错
    123 整数123
    3.14 浮点数3.14
    "123" 字符串类型的123
  2. raw_input()

    将所有的输入识别为字符串。

而 python 3 只有input()函数且等价于 python 2 的raw_input()函数。

运算符

除法

python 2 中除法a/b

  • 若a、b都是整数则地板除,结果向下取整(结果为负时,取小于该结果的第一个负整数),例如(-5/2=-3)。
  • 若a、b至少一个为浮点数,则结果也是相对精确的浮点数;对于a//b,无论a、b为何种数字类型结果都向下取整,只是a、b有浮点数时结果取整后以浮点数形式显示(5.0//2=2.0)。

python 3 中a/b默认结果总为浮点数,a//b与python2一致。

不等运算符

Python 2.x 中不等于有两种写法!=<>,Python 3.x 中去掉了<>, 只有!=一种写法。

八进制的表示方式

Python 2 可以用0开头或者0o开头表示八进制,python 3 只能用0o开头表示。

数据类型

删除 long 类型

python 3 删除了long类型,现在只有一种整型——int,但它的行为就像 2.x 版本的long

新增 bytes 类型

python 3 新增了二进制序列类型 bytes。bytes 对象是由单个字节构成的不可变序列,即字符经过编码的以八位二进制为单位的序列。

另外,在 Python 2.x 系列中,允许 8 位字符串( 2.x 所提供的最接近内置二进制数据类型的对象)与 Unicode 字符串进行各种隐式转换。 这是为了实现向下兼容的变通做法,以适应 Python 最初只支持 8 位文本而 Unicode 文本是后来才被加入这一事实。 在 Python 3.x 中,这些隐式转换已被取消 —— 8 位二进制数据与 Unicode 文本间的转换必须显式地进行,bytes 与字符串对象的比较结果将总是不相等。

返回迭代器

map、filter 和 reduce 等高阶函数返回值的类型由列表改为迭代器,另外 reduce 函数在 Python 3.x 中已经不属于 built-in 了,被挪到 functools 模块当中。

返回视图对象

字典的keys()values()items()方法在 python 2 中返回列表,在 python 3 中返回可迭代的视图对象。

关于类

经典类与新式类

python 2 允许创建一个经典类,即不继承任何类,包括object类。创建新式类要显式继承父类。

python 3 不再支持经典类,若定义时不显式继承任何类,则默认继承object类。

特殊函数 super

python 2 使用super()在子类方法关联父类对应方法时必须要传递父类名和子类的selfsuper()作为参数,而在 python 3 可以省略super()的参数。

class C(B):
    def method(self, arg):
        # super().父类对应方法(父类的参数)
        super().method(arg)
        # 上一句写法在Python3等价于如下语句
        # super(C, self).method(arg)

删除 repr 表达式

Python 2.x 中反引号``相当于repr函数的作用,返回一个字符串对象。

Python 3.x 中去掉了这种写法,只允许使用repr函数。

range 与 xrange

python 2.x 中range函数返回一个数字列表,而xrange函数返回 xrange 对象,它表示一个不可变的数字序列。xrange 对象相比常规 list 或 tuple 的优势在于一个 xrange 对象总是占用固定数量的(较小)内存,不论其所表示的范围有多大(因为它只保存了 start, stop 和 step 值,并会根据需要计算具体单项或子范围的值)。这一点与生成器的惰性计算有点相似,它是可迭代对象但并不是生成器或者迭代器。

python 3.x 中只保留了range函数,它与xrange十分相似,但是支持了更多功能,如 3.2 支持了切片操作。

另外,在 3.3 版更改: 定义 '==' 和 '!=' 以根据 range 对象所定义的值序列来进行比较(而不是根据对象的标识)。也就是说,如果两个 range 对象表示相同的值序列就认为它们是相等的。请注意比较结果相等的两个 range 对象可能会具有不同的 start,stop 和 step 属性,例如 range(0) == range(2, 1, 3)range(0, 3, 2) == range(0, 4, 2)

异常

在 Python 3 中处理异常也轻微的改变了,在 Python 3 中我们现在使用 as 作为关键词。

捕获异常的语法由 except exc, var 改为 except exc as var

使用语法except (exc1, exc2) as var 可以同时捕获多种类别的异常。 Python 2.6 已经支持这两种语法。

  • 在 2.x 版本,所有类型的对象都是可以被直接抛出的,在 3.x 时代,只有继承自BaseException的对象才可以被抛出。
  • 在 2.x 版本raise 语句使用逗号将抛出对象类型和参数分开,3.x 取消了这种奇葩的写法,直接调用构造函数抛出对象即可。如raise TypeError('MyRange expected 1 arguments, got 0')

布尔值

TrueFalse 在 Python 2 中是两个全局变量,既然是变量,那么他们就可以指向其它对象,例如:

# python 2.x
print True
print False
True = 123
False = 321
print True
print False
# 输出结果如下:
# True
# False
# 123
# 321

Python 3 修正了这个缺陷,True 和 False 变为两个关键字,永远指向两个固定的对象,不允许再被重新赋值。

新增 nonlocal 语句

nonlocal 语句会使得所列出的名称指向之前在最近的包含作用域中绑定的除全局变量以外的变量。例如内层函数的局部变量n绑定外层函数的局部变量n。 这种功能很重要,因为绑定的默认行为是先搜索局部命名空间。 这个语句允许被封装的代码重新绑定局部作用域以外且非全局(模块)作用域当中的变量。

回收列表推导式的变量

在 python 2中,如下列表推导式中 for 语句的变量 x 在后面的代码中可被使用:

y = [x for x in xrange(10)]
print x
print y
# 输出如下:
# 9
# [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

而 python 3,变量 x 作为局部变量在列表推导式执行结束后就被回收了,后续无法访问。

y = [x for x in range(10)]
print(x)
print(y)
# 输出:
# Traceback (most recent call last):
#   File "<input>", line 2, in <module>
# NameError: name 'x' is not defined

不可排序类型的报错

当对不可排序类型做比较的时候,python 2 会返回 False,python 3 会抛出一个类型错误:

# python 2.x
print [1, 2, 3] > 'abc'
# False

# python 3.x
print([1, 2, 3] > 'abc')
# Traceback (most recent call last):
#   File "E:/CS_Practice/python/2021/main.py", line 4, in <module>
#     print([1, 2, 3] > 'abc')
# TypeError: '>' not supported between instances of 'list' and 'str'

@zhangliwen
Copy link
Owner Author

gh-md-toc

gh-md-toc — is for you if you want to generate TOC (Table Of Content) for a README.md or
a GitHub wiki page without installing additional software.

It's my try to fix a problem:

gh-md-toc is able to process:

  • stdin
  • local files (markdown files in local file system)
  • remote files (html files on github.com)

gh-md-toc tested on Ubuntu, and macOS High Sierra (gh-md-toc release 0.4.9). If you want it on Windows, you
better to use a golang based implementation:

It's more solid, reliable and with ability of a parallel processing. And
absolutely without dependencies.

Build Status

Table of contents

Installation

Linux (manual installation)

$ wget https://raw.githubusercontent.com/ekalinin/github-markdown-toc/master/gh-md-toc
$ chmod a+x gh-md-toc

MacOS (manual installation)

$ curl https://raw.githubusercontent.com/ekalinin/github-markdown-toc/master/gh-md-toc -o gh-md-toc
$ chmod a+x gh-md-toc

Linux or MacOS (using Basher)

$ basher install ekalinin/github-markdown-toc
# `gh-md-toc` will automatically be available in the PATH

Usage

STDIN

Here's an example of TOC creating for markdown from STDIN:

➥ cat ~/projects/Dockerfile.vim/README.md | ./gh-md-toc -
  * [Dockerfile.vim](#dockerfilevim)
  * [Screenshot](#screenshot)
  * [Installation](#installation)
        * [OR using Pathogen:](#or-using-pathogen)
        * [OR using Vundle:](#or-using-vundle)
  * [License](#license)

Local files

Here's an example of TOC creating for a local README.md:

➥ ./gh-md-toc ~/projects/Dockerfile.vim/README.md


Table of Contents
=================

  * [Dockerfile.vim](#dockerfilevim)
  * [Screenshot](#screenshot)
  * [Installation](#installation)
        * [OR using Pathogen:](#or-using-pathogen)
        * [OR using Vundle:](#or-using-vundle)
  * [License](#license)

Remote files

And here's an example, when you have a README.md like this:

And you want to generate TOC for it.

There is nothing easier:

➥ ./gh-md-toc https://github.com/ekalinin/envirius/blob/master/README.md

Table of Contents
=================

  * [envirius](#envirius)
    * [Idea](#idea)
    * [Features](#features)
  * [Installation](#installation)
  * [Uninstallation](#uninstallation)
  * [Available plugins](#available-plugins)
  * [Usage](#usage)
    * [Check available plugins](#check-available-plugins)
    * [Check available versions for each plugin](#check-available-versions-for-each-plugin)
    * [Create an environment](#create-an-environment)
    * [Activate/deactivate environment](#activatedeactivate-environment)
      * [Activating in a new shell](#activating-in-a-new-shell)
      * [Activating in the same shell](#activating-in-the-same-shell)
    * [Get list of environments](#get-list-of-environments)
    * [Get current activated environment](#get-current-activated-environment)
    * [Do something in environment without enabling it](#do-something-in-environment-without-enabling-it)
    * [Get help](#get-help)
    * [Get help for a command](#get-help-for-a-command)
  * [How to add a plugin?](#how-to-add-a-plugin)
    * [Mandatory elements](#mandatory-elements)
      * [plug_list_versions](#plug_list_versions)
      * [plug_url_for_download](#plug_url_for_download)
      * [plug_build](#plug_build)
    * [Optional elements](#optional-elements)
      * [Variables](#variables)
      * [Functions](#functions)
    * [Examples](#examples)
  * [Example of the usage](#example-of-the-usage)
  * [Dependencies](#dependencies)
  * [Supported OS](#supported-os)
  * [Tests](#tests)
  * [Version History](#version-history)
  * [License](#license)
  * [README in another language](#readme-in-another-language)

That's all! Now all you need — is copy/paste result from console into original
README.md.

If you do not want to copy from console you can add > YOURFILENAME.md at the end of the command like ./gh-md-toc https://github.com/ekalinin/envirius/blob/master/README.md > table-of-contents.md and this will store the table of contents to a file named table-of-contents.md in your current folder.

And here is a result:

Moreover, it's able to work with GitHub's wiki pages:

➥ ./gh-md-toc https://github.com/ekalinin/nodeenv/wiki/Who-Uses-Nodeenv

Table of Contents
=================

  * [Who Uses Nodeenv?](#who-uses-nodeenv)
    * [OpenStack](#openstack)
    * [pre-commit.com](#pre-commitcom)

Multiple files

It supports multiple files as well:

➥ ./gh-md-toc \
    https://github.com/aminb/rust-for-c/blob/master/hello_world/README.md \
    https://github.com/aminb/rust-for-c/blob/master/control_flow/README.md \
    https://github.com/aminb/rust-for-c/blob/master/primitive_types_and_operators/README.md \
    https://github.com/aminb/rust-for-c/blob/master/unique_pointers/README.md

  * [Hello world](https://github.com/aminb/rust-for-c/blob/master/hello_world/README.md#hello-world)

  * [Control Flow](https://github.com/aminb/rust-for-c/blob/master/control_flow/README.md#control-flow)
    * [If](https://github.com/aminb/rust-for-c/blob/master/control_flow/README.md#if)
    * [Loops](https://github.com/aminb/rust-for-c/blob/master/control_flow/README.md#loops)
    * [For loops](https://github.com/aminb/rust-for-c/blob/master/control_flow/README.md#for-loops)
    * [Switch/Match](https://github.com/aminb/rust-for-c/blob/master/control_flow/README.md#switchmatch)
    * [Method call](https://github.com/aminb/rust-for-c/blob/master/control_flow/README.md#method-call)

  * [Primitive Types and Operators](https://github.com/aminb/rust-for-c/blob/master/primitive_types_and_operators/README.md#primitive-types-and-operators)

  * [Unique Pointers](https://github.com/aminb/rust-for-c/blob/master/unique_pointers/README.md#unique-pointers)

Combo

You can easily combine both ways:

➥ ./gh-md-toc \
    ~/projects/Dockerfile.vim/README.md \
    https://github.com/ekalinin/sitemap.s/blob/master/README.md

  * [Dockerfile.vim](~/projects/Dockerfile.vim/README.md#dockerfilevim)
  * [Screenshot](~/projects/Dockerfile.vim/README.md#screenshot)
  * [Installation](~/projects/Dockerfile.vim/README.md#installation)
        * [OR using Pathogen:](~/projects/Dockerfile.vim/README.md#or-using-pathogen)
        * [OR using Vundle:](~/projects/Dockerfile.vim/README.md#or-using-vundle)
  * [License](~/projects/Dockerfile.vim/README.md#license)

  * [sitemap.js](https://github.com/ekalinin/sitemap.js/blob/master/README.md#sitemapjs)
    * [Installation](https://github.com/ekalinin/sitemap.js/blob/master/README.md#installation)
    * [Usage](https://github.com/ekalinin/sitemap.js/blob/master/README.md#usage)
    * [License](https://github.com/ekalinin/sitemap.js/blob/master/README.md#license)

Created by [gh-md-toc](https://github.com/ekalinin/github-markdown-toc)

Auto insert and update TOC

Just put into a file these two lines:

<!--ts-->
<!--te-->

And run:

$ ./gh-md-toc --insert README.test.md

Table of Contents
=================

   * [gh-md-toc](#gh-md-toc)
   * [Installation](#installation)
   * [Usage](#usage)
      * [STDIN](#stdin)
      * [Local files](#local-files)
      * [Remote files](#remote-files)
      * [Multiple files](#multiple-files)
      * [Combo](#combo)
   * [Tests](#tests)
   * [Dependency](#dependency)

!! TOC was added into: 'README.test.md'
!! Origin version of the file: 'README.test.md.orig.2018-02-04_192655'
!! TOC added into a separate file: 'README.test.md.toc.2018-02-04_192655'


Created by [gh-md-toc](https://github.com/ekalinin/github-markdown-toc)

Now check the same file:

➜ grep -A15 "<\!\-\-ts" README.test.md
<!--ts-->
   * [gh-md-toc](#gh-md-toc)
   * [Table of contents](#table-of-contents)
   * [Installation](#installation)
   * [Usage](#usage)
      * [STDIN](#stdin)
      * [Local files](#local-files)
      * [Remote files](#remote-files)
      * [Multiple files](#multiple-files)
      * [Combo](#combo)
      * [Auto insert and update TOC](#auto-insert-and-update-toc)
   * [Tests](#tests)
   * [Dependency](#dependency)

<!-- Added by: <your-user>, at: 2018-02-04T19:38+03:00 -->

<!--te-->

Next time when your file will be changed just repeat the command (./gh-md-toc --insert ...) and TOC will be refreshed again.

GitHub token

All your tokens are here.

You will need them if you get an error like this:

Parsing local markdown file requires access to github API
Error: You exceeded the hourly limit. See: https://developer.github.com/v3/#rate-limiting
or place github auth token here: ./token.txt

A token can be used as an env variable:

➥ GH_TOC_TOKEN=2a2dab...563 ./gh-md-toc README.md

Table of Contents
=================

* [github\-markdown\-toc](#github-markdown-toc)
* [Table of Contents](#table-of-contents)
* [Installation](#installation)
* [Tests](#tests)
* [Usage](#usage)
* [LICENSE](#license)

Or from a file:

echo "2a2dab...563" > ./token.txt
➥ ./gh-md-toc README.md

Table of Contents
=================

* [github\-markdown\-toc](#github-markdown-toc)
* [Table of Contents](#table-of-contents)
* [Installation](#installation)
* [Tests](#tests)
* [Usage](#usage)
* [LICENSE](#license)

TOC generation with Github Actions

Config:

on:
  push:
    branches: [main]
    paths: ['foo.md']

jobs:
  build:
    runs-on: ubuntu-latest
    timeout-minutes: 5
    steps:
      - uses: actions/checkout@v2
      - run: |
          curl https://raw.githubusercontent.com/ekalinin/github-markdown-toc/master/gh-md-toc -o gh-md-toc
          chmod a+x gh-md-toc
          ./gh-md-toc --insert --no-backup foo.md
      - uses: stefanzweifel/git-auto-commit-action@v4
        with:
          commit_message: Auto update markdown TOC

Tests

Done with bats.
Useful articles:

How to run tests:

➥ make test                                                                                                                 

 ✓ TOC for local README.md
 ✓ TOC for remote README.md
 ✓ TOC for mixed README.md (remote/local)
 ✓ TOC for markdown from stdin
 ✓ --help
 ✓ --version

6 tests, 0 failures

Dependency

  • curl or wget
  • awk (mawk is not tested)
  • grep
  • sed
  • bats (for unit tests)

Tested on Ubuntu 14.04/14.10 in bash/zsh.

Docker

Local

  • Build
$ docker build -t markdown-toc-generator .
  • Run on an URL
$ docker run -it markdown-toc-generator https://github.com/ekalinin/envirius/blob/master/README.md
  • Run on a local file (need to share volume with docker)
$ docker run -it -v /data/ekalinin/envirius:/data markdown-toc-generator /data/README.md

Public

$ docker pull evkalinin/gh-md-toc:0.7.0

$ docker images | grep toc
evkalinin/gh-md-toc                       0.7.0 0b8db6aed298        11 minutes ago      147MB

$ docker run -it evkalinin/gh-md-toc:0.7.0 \
    https://github.com/ekalinin/envirius/blob/master/README.md

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant