Skip to content

Commit

Permalink
add a circleci integration: check unclosed tags (#3786)
Browse files Browse the repository at this point in the history
* test

* upload code and test file

* Update rocksdb-overview.md

* Update check-tags.py

* Update config.yml

* fix import

* test

* Update check-tags.py

* test

* final test

* delete git remote add

* add exit code 0 and 1

* suceess! more test

* move it in front

* Revert "move it in front"

This reverts commit 6056fb3.

* Update experimental-features-4.0.md

* modify outputs

* test toc

* cover more edge cases: tags with attricutes and self closed tags

* fix tag_is_wrapped judgement

* specify which file is being checked

* add git fetch upstream back to fix unsynced issue

* add ;

* fix edited files

* fix left/right backticks judgement

* control exit using status_code

* clean up output info

* clean up output info

* add filter frontmatter func

* clean up code comments

* Apply suggestions from code review

* minor fix

* remove chinese code comments

* filter copyable shortcodes

* fix match regex string and add a filter case; fix some tag issues in upstream
  • Loading branch information
yikeke authored Sep 3, 2020
1 parent 02e83f3 commit ca959a2
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ jobs:
command: |
scripts/verify-link-anchors.sh
- run:
name: "Check unclosed tags"
command:
python3 scripts/check-tags.py $(git diff-tree --name-only --no-commit-id -r upstream/master..HEAD -- '*.md' ':(exclude).github/*' ':(exclude)v1.0/*' ':(exclude)v2.0/*' ':(exclude)v2.1-legacy/*')

build:
docker:
- image: andelf/doc-build:0.1.9
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ Currently, we maintain the following versions of TiDB documentation in different

## Contributing

[<img src="media/contribution-map.png" alt="contribution-map" width="180">](https://github.com/pingcap/community/tree/master/special-interest-groups/sig-docs)
[<img src="media/contribution-map.png" alt="contribution-map" width="180"></img>](https://github.com/pingcap/community/tree/master/special-interest-groups/sig-docs)

See [TiDB Documentation Contributing Guide](/CONTRIBUTING.md) to become a contributor! 🤓
2 changes: 1 addition & 1 deletion best-practices/haproxy-best-practices.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ listen admin_stats # The name of the Stats page reportin
listen tidb-cluster # Database load balancing.
bind 0.0.0.0:3390 # The Floating IP address and listening port.
mode tcp # HAProxy uses layer 4, the transport layer.
balance leastconn # The server with the smallest number of connections receives the connection. `leastconn' is recommended where long sessions are expected, such as LDAP, SQL and TSE, rather than protocols using short sessions, such as HTTP. The algorithm is dynamic, which means that server weights might be adjusted on the fly for slow starts for instance.
balance leastconn # The server with the smallest number of connections receives the connection. "leastconn" is recommended where long sessions are expected, such as LDAP, SQL and TSE, rather than protocols using short sessions, such as HTTP. The algorithm is dynamic, which means that server weights might be adjusted on the fly for slow starts for instance.
server tidb-1 10.9.18.229:4000 check inter 2000 rise 2 fall 3 # Detects port 4000 at a frequency of once every 2000 milliseconds. If it is detected as successful twice, the server is considered available; if it is detected as failed three times, the server is considered unavailable.
server tidb-2 10.9.39.208:4000 check inter 2000 rise 2 fall 3
server tidb-3 10.9.64.166:4000 check inter 2000 rise 2 fall 3
Expand Down
2 changes: 1 addition & 1 deletion functions-and-operators/expressions-pushed-down.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ tidb> explain select * from t where a < 2 and a > 2;
| = | eq |
| != | ne |
| `<>` | ne |
| <=> | nulleq |
| `<=>` | nulleq |
| &#124; | bitor |
| && | bitand|
| &#124;&#124; | or |
Expand Down
4 changes: 2 additions & 2 deletions functions-and-operators/operators.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ This document describes the operators precedence, comparison functions and opera
| [DIV](https://dev.mysql.com/doc/refman/5.7/en/arithmetic-functions.html#operator_div) | Integer division |
| [/](https://dev.mysql.com/doc/refman/5.7/en/arithmetic-functions.html#operator_divide) | Division operator |
| [=](https://dev.mysql.com/doc/refman/5.7/en/comparison-operators.html#operator_equal) | Equal operator |
| [<=>](https://dev.mysql.com/doc/refman/5.7/en/comparison-operators.html#operator_equal-to) | NULL-safe equal to operator |
| [`<=>`](https://dev.mysql.com/doc/refman/5.7/en/comparison-operators.html#operator_equal-to) | NULL-safe equal to operator |
| [>](https://dev.mysql.com/doc/refman/5.7/en/comparison-operators.html#operator_greater-than) | Greater than operator |
| [>=](https://dev.mysql.com/doc/refman/5.7/en/comparison-operators.html#operator_greater-than-or-equal) | Greater than or equal operator |
| [IS](https://dev.mysql.com/doc/refman/5.7/en/comparison-operators.html#operator_is) | Test a value against a boolean |
Expand Down Expand Up @@ -91,7 +91,7 @@ For details, see [Operator Precedence](https://dev.mysql.com/doc/refman/5.7/en/o
| [BETWEEN ... AND ...](https://dev.mysql.com/doc/refman/5.7/en/comparison-operators.html#operator_between) | Check whether a value is within a range of values |
| [COALESCE()](https://dev.mysql.com/doc/refman/5.7/en/comparison-operators.html#function_coalesce) | Return the first non-NULL argument |
| [=](https://dev.mysql.com/doc/refman/5.7/en/comparison-operators.html#operator_equal) | Equal operator |
| [<=>](https://dev.mysql.com/doc/refman/5.7/en/comparison-operators.html#operator_equal-to) | NULL-safe equal to operator |
| [`<=>`](https://dev.mysql.com/doc/refman/5.7/en/comparison-operators.html#operator_equal-to) | NULL-safe equal to operator |
| [>](https://dev.mysql.com/doc/refman/5.7/en/comparison-operators.html#operator_greater-than) | Greater than operator |
| [>=](https://dev.mysql.com/doc/refman/5.7/en/comparison-operators.html#operator_greater-than-or-equal) | Greater than or equal operator |
| [GREATEST()](https://dev.mysql.com/doc/refman/5.7/en/comparison-operators.html#function_greatest) | Return the largest argument |
Expand Down
124 changes: 124 additions & 0 deletions scripts/check-tags.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
import re
import sys

# reference: https://stackoverflow.com/questions/35761133/python-how-to-check-for-open-and-close-tags
def stack_tag(tag, stack):
t = tag[1:-1]
first_space = t.find(' ')
#print(t)
if t[-1:] == '/':
self_closed_tag = True
elif t[:1] != '/':
# Add tag to stack
if first_space == -1:
stack.append(t)
# print("TRACE open", stack)
else:
stack.append(t[:first_space])
# print("TRACE open", stack)
else:
if first_space != -1:
t = t[1:first_space]
else:
t = t[1:]

if len(stack) == 0:
# print("No blocks are open; tried to close", t)
closed_tag = True
else:
if stack[-1] == t:
# Close the block
stack.pop()
# print("TRACE close", t, stack)
else:
# print("Tried to close", t, "but most recent open block is", stack[-1])
if t in stack:
stack.remove(t)
# print("Prior block closed; continuing")

# if len(stack):
# print("Blocks still open at EOF:", stack)
return stack

def tag_is_wrapped(pos, content):
tag_start = pos[0]
tag_end = pos[1]
content_previous = content[:tag_start][::-1] # reverse content_previous
content_later = content[tag_end:]

left_wraps_findall = re.findall(r'`', content_previous)
left_single_backtick = len(left_wraps_findall) % 2
right_wraps_findall = re.findall(r'`', content_later)
right_single_backtick = len(right_wraps_findall) % 2
# print(left_single_backtick, right_single_backtick)

if left_single_backtick != 0 and right_single_backtick != 0:
# print(content_previous.find('`'), content_later.find('`'))
# print(content_previous)
# print(content_later)
return True
else:
# print(content_previous.find('`'), content_later.find('`'))
# print(content_previous)
# print(content_later)
return False

def filter_content(content):
content_findall = re.findall(r'\n---\n', content)
if len(content_findall):
content_finditer = re.finditer(r'\n---\n', content)
for i in content_finditer:
meta_pos = i.span()[1]
# print(content[meta_pos:])
return content[meta_pos:]
else:
return content

status_code = 0

# print(sys.argv[1:])
for filename in sys.argv[1:]:
#print("Checking " + filename + "......\n")
file = open(filename, "r" )
content = file.read()
file.close()

content = filter_content(content)
result_findall = re.findall(r'<([^\n`>]*)>', content)
if len(result_findall) == 0:
# print("The edited markdown file " + filename + " has no tags!\n")
status_code = 0
else:
result_finditer = re.finditer(r'<([^\n`>]*)>', content)
stack = []
for i in result_finditer:
# print(i.group(), i.span())
tag = i.group()
pos = i.span()

if tag[:4] == '<!--' and tag[-3:] == '-->':
continue
elif content[pos[0]-2:pos[0]] == '{{' and content[pos[1]:pos[1]+2] == '}}':
# print(tag) # filter copyable shortcodes
continue
elif tag[:5] == '<http': # or tag[:4] == '<ftp'
# filter urls
continue
elif tag_is_wrapped(pos, content):
# print(content[int(pos[0])-1:int(pos[1]+1)])
# print(tag, 'is wrapped by backticks!')
continue

stack = stack_tag(tag, stack)

if len(stack):
stack = ['<' + i + '>' for i in stack]
print("ERROR: " + filename + ' has unclosed tags: ' + ', '.join(stack) + '.\n')
status_code = 1
else:
# print("The edited markdown file has tags. But all tags are closed, congratulations!\n")
status_code = 0

if status_code:
print("HINT: Unclosed tags will cause website build failure. Please fix the reported unclosed tags. You can use backticks `` to wrap them or close them. Thanks.")
exit(1)

0 comments on commit ca959a2

Please sign in to comment.