Skip to content

Commit 4cb6459

Browse files
authored
Merge pull request #17 from canonical/update-docs
feat: Update README.md
2 parents 3b82053 + 3ff7b0e commit 4cb6459

File tree

3 files changed

+96
-8
lines changed

3 files changed

+96
-8
lines changed

README.md

Lines changed: 93 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,109 @@ Flask extension to parse websites and extract structured data to build sitemaps.
44
## Install
55
Install the project with pip: `pip install canonicalwebteam.directory-parser`
66

7-
You can add the extension on your project by doing the following:
87

8+
## Using the directory parser
9+
10+
### Sitemap templates
11+
Include sitemap templates in your Flask app. Copy the following codeblock to where your application is instantiated e.g `app.py`. The template loader should be placed right after the app is instantiated.
12+
13+
```
14+
from jinja2 import ChoiceLoader, FileSystemLoader
15+
from pathlib import Path
16+
import canonicalwebteam.directory_parser as directory_parser
17+
18+
19+
# Set up Flask application
20+
app = FlaskBase(...)
21+
22+
23+
# Include directory parser templates
24+
directory_parser_templates = (
25+
Path(directory_parser.__file__).parent / "templates"
26+
)
27+
28+
loader = ChoiceLoader(
29+
[
30+
FileSystemLoader(str(directory_parser_templates)),
31+
]
32+
)
33+
34+
app.jinja_loader = loader
935
```
10-
from canonicalwebteam.directory_parser import scan_directory
1136

12-
node = scan_directory("<example-templates-path>)
37+
### Generate sitemaps
38+
The `generate_sitemap` function will generate a sitemap given directory path and base url using the sitemap templates.
39+
1340
```
41+
# Dynamic sitemaps that do not need to be included in the sitemap tree.
42+
# Differ from project to project, can be checked on /sitemap.xml
43+
DYNAMIC_SITEMAPS = [
44+
"tutorials",
45+
"engage",
46+
"ceph/docs",
47+
"blog",
48+
"security/notices",
49+
"security/cves",
50+
"security/livepatch/docs",
51+
"robotics/docs",
52+
]
53+
54+
directory_path = os.getcwd() + "/templates"
55+
base_url = "https://ubuntu.com"
56+
57+
xml_sitemap = directory_parser.generate_sitemap(
58+
directory_path,
59+
base_url,
60+
exclude_paths=DYNAMIC_SITEMAPS
61+
)
62+
63+
if xml_sitemap:
64+
with open(sitemap_path, "w") as f:
65+
f.write(xml_sitemap)
66+
67+
# Serve the existing sitemap
68+
with open(sitemap_path, "r") as f:
69+
xml_sitemap = f.read()
70+
71+
response = flask.make_response(xml_sitemap)
72+
response.headers["Content-Type"] = "application/xml"
73+
return response
74+
```
75+
76+
### Parse project directory tree
77+
If you'd like to get the parsed tree of a given directory, you can use the `scan_directory` function.
78+
79+
```
80+
directory_path = os.getcwd() + "/templates"
81+
tree = directory_parser.scan_directory(
82+
directory_path, exclude_paths=DYNAMIC_SITEMAPS
83+
)
84+
```
85+
`tree` will return a tree of all the templates given in the `directory_path`
1486

15-
`node` will return a tree of all the templates given in the `<example-templates-path>
1687

1788
## Local development
89+
### Running the project
90+
This guide assumes that you are using [dotrun](https://github.com/canonical/dotrun/) to run your Flask app.
91+
92+
#### Include a relative path to the project
93+
This example assumes both project exist in the same directory
94+
95+
In `requirements.txt`:
96+
```
97+
# Comment out package import
98+
# canonicalwebteam.directory-parser==1.2.6
99+
100+
-e ../directory-parser
101+
```
102+
103+
#### Run project with a mounted additor
104+
`dotrun -m /path/to/canonicalwebteam.directory-parser:../directory-parser`
105+
18106

19107
### Linting and formatting
20108

21-
Tests can be run with Tox:
109+
To follow the standard linting rules of this project, we are using [Tox](https://tox.wiki/en/latest/)
22110
```
23111
pip3 install tox # Install tox
24112
tox -e lint # Check the format of Python code

canonicalwebteam/directory_parser/app.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -373,8 +373,8 @@ def scan_directory(path_name, exclude_paths=None, base=None):
373373

374374
def generate_sitemap(directory_path, base_url, exclude_paths=None):
375375
"""
376-
Generate sitemap given a directory path and a base url. It uses the
377-
local project's templates to generate the sitemap.
376+
Generate sitemap given a directory path and a base url using
377+
the sitemap templates.
378378
"""
379379
tree = scan_directory(directory_path, exclude_paths)
380380
xml_sitemap = flask.render_template(

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
setup(
66
name="canonicalwebteam.directory-parser",
7-
version="1.2.5",
7+
version="1.2.7",
88
author="Canonical webteam",
99
author_email="[email protected]",
1010
url="https://github.com/canonical/canonicalwebteam.directory-parser",

0 commit comments

Comments
 (0)