-
Notifications
You must be signed in to change notification settings - Fork 198
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
helper: split_markdown_front_matter #443
Conversation
f80d122
to
209a853
Compare
This looks good to me, could you swap this with the thing we have in istio and run tests. That way we'll cover some more testcases and the refactor would also be done. |
As far as the alternative approaches are concerned, I would stick with something dumb and simple (implementation wise). |
209a853
to
a0473c6
Compare
Done.
This PR actually originally offers the same code with a minor improvement.
I'm not aware of any instance where front matter doesn't start at the very beginning. I looked at some implementations and it appears that it is rather a strict requirement. (Opening fence must begin on the first line of the markdown string/file ref) |
@Hritik14 thanks for the research and the refactor. Let's use the one you suggested in the comments then. Though the current implementation is concise, it's not super obvious. |
a0473c6
to
9d17162
Compare
@sbs2001 I've updated it with the one in comments though I'd really appreciate @pombredanne's insight here as he was really in favor of the |
9d17162
to
2fb4838
Compare
@sbs2001 @pombredanne ping |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! See some comments inline for your consideration.
vulnerabilities/helpers.py
Outdated
|
||
lines = lines.replace("\r\n", "\n") | ||
for index, line in enumerate(lines.split("\n")): | ||
if index == 0 and line.strip().startswith("---"): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I understand what you are doing here with your "splitter" variable and this is sleek. But this may not be easy to read and understand at least it was not obvious to me at first.
What could you do to make this easier to read?
- the first test can be done outside of the loop since if the document starts with
---
it must be on the first line which can be safely discarded (the first line). - with that done, then the only thing left is that this is frontmatter until the line is
---
and at this stage you switch to markdown/body. Or you could partition the remaining joined text on---
?
Or may be at least finding a better name than splitter
would help? something like bucket?
Here are some alternative ways for your consideration:
frontmatter = []
markdown = []
lines = text.splitlines(False)
if lines[0] == "---":
lines = lines[1:]
current_bucket = frontmatter
for line in lines:
if line == "---":
current_bucket = markdown
continue
current_bucket.append(line)
or this one not using any temp lists:
lines = text.splitlines(False)
# discard the first separator
if lines[0] == "---":
lines = lines[1:]
text = "\n".join(lines)
frontmatter, _, markdown = text.partition("---\n")
The point I am trying to make is that the important thing is to make the code readable and easy to understand such that 6 months from now you or anyone can get what you meant easily. So naming and being explicit and clear matters a lot IMHO.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I took your second implementation and now it looks like
lines = text.splitlines()
if lines[0] == "---":
lines = lines[1:]
text = "\n".join(lines)
frontmatter, _, markdown = text.partition("\n---\n")
return frontmatter, markdown
return "", text
Let me know what you think
In case the loop method is favorable, I'll make the necessary amends.
review: aboutcode-org#443 (review) Signed-off-by: Hritik Vijay <hritikxx8@gmail.com>
2fb4838
to
d1de06e
Compare
Thanks @pombredanne. I've incorporated the latest changes. Do have a look. |
Better documentation and more readable function structrue review: aboutcode-org#443 (review) Signed-off-by: Hritik Vijay <hritikxx8@gmail.com>
d1de06e
to
39ad90b
Compare
Better documentation and more readable function structrue review: aboutcode-org#443 (review) Signed-off-by: Hritik Vijay <hritikxx8@gmail.com>
39ad90b
to
6f6d16a
Compare
Better documentation and more readable function structrue review: aboutcode-org#443 (review) Signed-off-by: Hritik Vijay <hritikxx8@gmail.com>
6f6d16a
to
7375ec0
Compare
@pombredanne ping |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See a minor nit FYI. Otherwise LGTM!
helper for istio and mozilla importers Signed-off-by: Hritik Vijay <hritikxx8@gmail.com>
Signed-off-by: Hritik Vijay <hritikxx8@gmail.com>
also, sort imports Signed-off-by: Hritik Vijay <hritikxx8@gmail.com>
Signed-off-by: Hritik Vijay <hritikxx8@gmail.com>
Better documentation and more readable function structrue review: aboutcode-org#443 (review) Signed-off-by: Hritik Vijay <hritikxx8@gmail.com>
Signed-off-by: Hritik Vijay <hritikxx8@gmail.com>
cad873e
to
9f3e1ef
Compare
Better documentation and more readable function structrue review: aboutcode-org#443 (review) Signed-off-by: Hritik Vijay <hritikxx8@gmail.com>
helper for istio and mozilla importers
The following two alternatives are also proposed:
and the one present here under MPL license.
Signed-off-by: Hritik Vijay hritikxx8@gmail.com