Skip to content

Commit

Permalink
add default to extract()
Browse files Browse the repository at this point in the history
  • Loading branch information
javierluraschi committed Jul 25, 2024
1 parent e124c82 commit 1c71acd
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 2.5.2

- Add `default` parameter to `extract()` to return default when code not found

## 2.5.1

- Add support for `files` in `save()` to persist multiple files
Expand Down
4 changes: 3 additions & 1 deletion python/hal9/code.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import re

def extract(markdown, language):
def extract(markdown, language, default = None):
pattern = re.compile(rf'```{language}\s+(.*?)```', re.DOTALL)
code_blocks = pattern.findall(markdown)
code = '\n'.join(code_blocks)
if len(code) == 0:
return default
return code
2 changes: 1 addition & 1 deletion python/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "hal9"
version = "2.5.1"
version = "2.5.2"
description = ""
authors = ["Javier Luraschi <[email protected]>"]
readme = "README.md"
Expand Down
1 change: 1 addition & 0 deletions website/reference/code.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ Extracts all `language` code blocks from `markdown` text
| --- | --- | --- |
| markdown | <code>String</code> | The markdown to extract blocks from. |
| language | <code>String</code> | The language of the code blocks. |
| default | <code>Any</code> | The default value when code not found, defaults to `None`. |

0 comments on commit 1c71acd

Please sign in to comment.