-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmdorrst.py
110 lines (86 loc) · 3.11 KB
/
mdorrst.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#!/usr/bin/env python3
"""Tell appart Markdown and reStructuredText.
"""
from __future__ import print_function, division, absolute_import
import re
__author__ = """Julien Palard"""
__email__ = "julien@palard.fr"
__version__ = "0.4.0"
def find_typical_markers(content):
"""Try to find typical markdown or rst markers:"""
markdown = {
"titles": len(re.findall("^#+ ", content, re.M)),
"links": content.count("]("),
"code_blocks": len(re.findall("^ *```$", content, re.M)),
"images": content.count("!["),
"ref_link": len(re.findall(r"^\[[0-9]\]:", content, re.M)),
}
restructuredtext = {
"titles": (
len(re.findall("^~{5,}$", content, re.M))
+ len(re.findall("^`{5,}$", content, re.M))
),
"compat_titles": (
len(re.findall("^={5,}$", content, re.M))
+ len(re.findall("^-{5,}$", content, re.M))
)
/ 1000,
"links": content.count("`_"),
"code": content.count(".. code"),
"images": content.count(".. image::"),
"block": content.count("::\n"),
"reference": len(re.findall("`[a-zA-Z0-9]+`_", content)),
"include": content.count(".. include::"),
"substitutions": content.count("\n.. |"),
}
return markdown, restructuredtext
def sniff(content):
"""Deduce the format (md|rst|txt) of a given string."""
markdown, restructuredtext = find_typical_markers(content)
markdown_points = sum(markdown.values())
restructuredtext_points = sum(restructuredtext.values())
if markdown_points == restructuredtext_points == 0:
return "txt"
return "md" if markdown_points > restructuredtext_points else "rst"
def parse_args(args):
import argparse
"""Parse command line parameters
Args:
args ([str]): command line parameters as list of strings
Returns:
:obj:`argparse.Namespace`: command line parameters namespace
"""
parser = argparse.ArgumentParser(
description="Tell appart rst or md for a given file."
)
parser.add_argument(
"--version", action="version", version="mdorrst {ver}".format(ver=__version__)
)
parser.add_argument(
"--verbose", "-v", action="store_true", help="Tell why I think like this."
)
parser.add_argument("file", help="Path of a file to test.")
return parser.parse_args(args)
def main(args):
"""Main entry point allowing external calls
Args:
args ([str]): command line parameter list
"""
args = parse_args(args)
if args.verbose:
import pprint
with open(args.file, encoding="UTF-8") as readme_file:
markdown, restructuredtext = find_typical_markers(readme_file.read())
print("Markdown points:")
pprint.pprint(markdown)
print("reStructuredText points:")
pprint.pprint(restructuredtext)
return
with open(args.file, encoding="UTF-8") as file_to_sniff:
print(sniff(file_to_sniff.read()))
def run():
"""Entry point for console_scripts"""
import sys
main(sys.argv[1:])
if __name__ == "__main__":
run()