Skip to content

Commit

Permalink
Merge branch 'main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
angelogladding authored Nov 30, 2023
2 parents cb2c64f + 371ef4d commit 0745cb9
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 27 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ jobs:
- name: Install library
run: poetry install --no-interaction
- name: Run tests
run: poetry run pytest
run: make tests
- uses: psf/black@stable
with:
options: "--check --verbose"
Expand Down Expand Up @@ -67,7 +67,7 @@ jobs:
- name: Install library
run: poetry install --no-interaction
- name: Run tests
run: poetry run pytest
run: make tests
- uses: psf/black@stable
with:
options: "--check --verbose"
Expand Down Expand Up @@ -100,7 +100,7 @@ jobs:
- name: Install library
run: poetry install --no-interaction
- name: Run tests
run: poetry run pytest
run: make tests
- uses: psf/black@stable
with:
options: "--check --verbose"
Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
install:
poetry install
test:
poetry run pytest
tests:
poetry run pytest -s -vv --doctest-modules --doctest-glob README*
publish:
poetry publish --build
lint:
Expand Down
66 changes: 44 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,63 +23,85 @@ pip install mf2py

Import the parser using:

```python
import mf2py
```pycon
>>> import mf2py

```

### Parse a File

Parse a file containing HTML:

```python
with open('file/content.html','r') as file:
obj = mf2py.parse(doc=file)
```pycon
>>> with open("example.html", "r") as file:
... mf2json = mf2py.parse(doc=file)
>>> mf2json
{'items': [{'type': ['h-entry'],
'properties': {'name': ['Hello'],
'content': [{'html': 'Just saying hi.',
'value': 'Just saying hi.'}]}}],
'rels': {},
'rel-urls': {},
'debug': {'description': 'mf2py - microformats2 parser for python',
'source': 'https://github.com/microformats/mf2py',
'version': '1.1.3',
'markup parser': 'html5lib'}}

```

### Parse a String

Parse string containing HTML content:

```python
content = '<article class="h-entry"><h1 class="p-name">Hello</h1></article>'
obj = mf2py.parse(doc=content)
```pycon
>>> content = '<article class="h-entry"><h1 class="p-name">Hello</h1></article>'
>>> mf2py.parse(doc=content)["items"]
[{'type': ['h-entry'], 'properties': {'name': ['Hello']}}]

```

### Parse a HTML Document Retrieved from a URL
### Parse an HTML Document Retrieved from a URL

Parse content from a URL:

```python
obj = mf2py.parse(url="http://tommorris.org/")
```pycon
>>> mf2json = mf2py.parse(url="https://indieweb.org")

```

### Extensions

Use `expose_dom=True` to expose the DOM of embedded properties.

---
### Parser Object

`parse` is a convenience method that actually delegates to
`mf2py.Parser` to do the real work. More sophisticated behaviors are
available by invoking the object directly.

### Format Options
```pycon
>>> p = mf2py.Parser()

```

#### JSON Output

Retrieve parsed microformats as a Python dictionary or JSON string:

```python
p = mf2py.Parser(...)
p.to_dict() # returns a python dictionary
p.to_json() # returns a JSON string
```pycon
>>> mf2dict = p.to_dict()
>>> mf2json = p.to_json()

```

### Filter by Microformat Type
#### Filter by Microformat Type

Filter by microformat type.

Filter by microformat type:
```pycon
>>> dict_entries = p.to_dict(filter_by_type="h-entry")
>>> json_entries = p.to_json(filter_by_type="h-entry")

```python
p.to_dict(filter_by_type="h-entry")
p.to_json(filter_by_type="h-entry")
```

## Breaking Changes in v2
Expand Down
12 changes: 12 additions & 0 deletions example.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!doctype html>
<html lang=en-us>
<head>
<base href=https://example.com>
</head>
<body>
<div class=h-entry>
<h1 class=p-name>Hello</h1>
<div class=e-content>Just saying hi.</div>
</div>
</body>
</html>
2 changes: 2 additions & 0 deletions pytest.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[pytest]
doctest_optionflags = NORMALIZE_WHITESPACE IGNORE_EXCEPTION_DETAIL

0 comments on commit 0745cb9

Please sign in to comment.