1.2.7. 显示代码#
总得来说,codeblock代码块指令有两种形式,一种是直接将代码放入代码块中,另一种是引用已有的文件的代码将其放入代码块中。
语法高亮支持的语言
支持以下值:
none
(无突出显示)default
(类似于 python3 但却退缩到 none 不带警告的突出显示失败;默认情况下 highlight_language 未设置)guess
(让Pygments根据内容猜测lexer,只适用于某些可识别的语言)python
rest
c
java
… 以及其他语法高亮 Pygments supports
代码形式:
前者:
.. code-block:: code_language_type
后者:
.. literalinclude:: code_file_path(local or internet)
1.2.7.1. 使用codeblock#
.. code-block:: java
public class HelloWorld {
public static void main(String[] args){
System.out.println("Hello World!");
}
}
运行效果如下:
public class HelloWorld {
public static void main(String[] args){
System.out.println("Hello World!");
}
}
1.2.7.2. 为代码块添加标题和 label#
public class HelloWorld {
public static void main(String[] args){
System.out.println("Hello World!");
}
}
.. code-block:: java
:caption: 代码块添加标题
:name: HelloWorldExampleCodeBlock
public class HelloWorld {
public static void main(String[] args){
System.out.println("Hello World!");
}
}
1.2.7.3. 显示行号#
.. code-block:: java
:linenos:
public class HelloWorld {
public static void main(String[] args){
System.out.println("Hello World!");
}
}
运行效果如下:
1 2 3 4 5 | public class HelloWorld { public static void main(String[] args){ System.out.println("Hello World!"); } } |
1.2.7.4. 突出特定行#
.. code-block:: java
:emphasize-lines: 1,3-5
:linenos:
public class HelloWorld {
public static void main(String[] args){
System.out.println("Hello World!");
}
}
突出3到5行的运行效果如下:
1 2 3 4 5 | public class HelloWorld { public static void main(String[] args){ System.out.println("Hello World!"); } } |
1.2.7.5. 引用一个文件 literalinclude#
.. literalinclude:: ../example/Not_regular_expression.py
:language: python
:linenos:
:lines: 1-2,30-
运行结果如下;显示文件的第1到2行,以及30行之后的代码:
1 2 3 4 5 6 7 8 9 | # don't using regular expression to judge phone number # which like '415-555-4242' if __name__ == '__main__': number = input("Enter the number to see if it conforms to the rules: ") print("The number " + number + " is", end="") if isPhoneNumber(number): print(" a phone number.") else: print(" not a phone number.") |
1.2.7.6. 指定引用文件的方法,或类#
指令支持包含文件的一部分. 例如 Python模块, 可以选择类,函数或方法,使用 pyobject 选项。
.. literalinclude:: ../example/Not_regular_expression.py
:pyobject: isPhoneNumber
运行效果如下:
def isPhoneNumber(num: str) -> bool:
"""
don't using regular expression to judge phone number
which like '415-555-4242'
:param num: 字符串号码
:return: 字符串是否为电话的真假
"""
if len(num) != 12:
return False
for i in range(3):
if not num[i].isdecimal():
return False
if num[3] != '-':
return False
for i in range(4, 7):
if not num[i].isdecimal():
return False
if num[7] != "-":
return False
for i in range(8, 12):
if not num[i].isdecimal():
return False
return True
1.2.7.7. diff2个文件#
.. literalinclude:: ../example/Not_regular_expression.py
:diff: ../example/Not_regular_expression2.py
运行效果如下:
--- /home/docs/checkouts/readthedocs.org/user_builds/hz-sphinx-study/checkouts/stable/source/sptools/sphinx/example/Not_regular_expression2.py
+++ /home/docs/checkouts/readthedocs.org/user_builds/hz-sphinx-study/checkouts/stable/source/sptools/sphinx/example/Not_regular_expression.py
@@ -1,4 +1,5 @@
-
+# don't using regular expression to judge phone number
+# which like '415-555-4242'
def isPhoneNumber(num: str) -> bool:
"""