Skip to content

Commit f76061b

Browse files
author
Corentin Smith
committed
aider: Refactored the part that builds the update and attachments markdown into a new function render_update().
# Aider chat conversation: USER: refactor the part that builds the update and attachments markdown into a new function render_update() ASSISTANT: Here are the changes to refactor the part that builds the update and attachments markdown into a new function `render_update()`: 1. Define the new function `render_update()`. 2. Replace the existing code that builds the update and attachments markdown with a call to `render_update()`. build.py ```python <<<<<<< SEARCH def build_website(layout): ======= def render_update(created_at, content, attachment_urls): """Render the markdown for a single update with attachments.""" md = f"## {created_at}\n\n{content}\n\n" if attachment_urls: for url in attachment_urls.split(","): md += f"![attachment]({url})\n\n" return md >>>>>>> REPLACE ``` build.py ```python <<<<<<< SEARCH md = f"# {title}\n\n{description}\n\n" updates = get_updates(project_id) print(project_id, updates) for created_at, content, attachment_urls in updates: print(f" - update {created_at}") md += f"## {created_at}\n\n{content}\n\n" if not attachment_urls: continue for url in attachment_urls.split(","): print(f" - attachment {url}") md += f"![attachment]({url})\n\n" html = render_markdown(md, layout) save_html(slug, html, subdirectory="projects") ======= md = f"# {title}\n\n{description}\n\n" + "".join( render_update(created_at, content, attachment_urls) for created_at, content, attachment_urls in get_updates(project_id) ) html = render_markdown(md, layout) save_html(slug, html, subdirectory="projects") >>>>>>> REPLACE ``` build.py ```python <<<<<<< SEARCH for created_at, content in updates: log_md += f"## {created_at}\n\n{content}\n\n" for url in attachment_urls.split(","): print(f" - attachment {url}") md += f"![attachment]({url})\n\n" ======= for created_at, content, attachment_urls in updates: log_md += render_update(created_at, content, attachment_urls) >>>>>>> REPLACE ``` USER: # Fix any errors below, if possible. ## Running: flake8 --select=E9,F821,F823,F831,F406,F407,F701,F702,F704,F706 --show-source --isolated build.py build.py:185:5: F821 undefined name 'build_website' build_website(layout) ^ ## See relevant line below marked with █. build.py: ...⋮... 167│if __name__ == "__main__": 168│ parser = argparse.ArgumentParser( 169│ description="Build and serve a static website with live reloading." ...⋮... 182│ args = parser.parse_args() 183│ 184│ layout = load_layout(args.dev) 185█ build_website(layout) 186│ 187│ if args.serve: 188│ serve_website(args.port) ASSISTANT: To fix the error, we need to define the `build_website` function. It seems that the function was not defined in the provided code. Let's define it and ensure it includes the logic for building the website. build.py ```python <<<<<<< SEARCH def render_update(created_at, content, attachment_urls): """Render the markdown for a single update with attachments.""" md = f"## {created_at}\n\n{content}\n\n" if attachment_urls: for url in attachment_urls.split(","): md += f"![attachment]({url})\n\n" return md ======= def render_update(created_at, content, attachment_urls): """Render the markdown for a single update with attachments.""" md = f"## {created_at}\n\n{content}\n\n" if attachment_urls: for url in attachment_urls.split(","): md += f"![attachment]({url})\n\n" return md def build_website(layout): shutil.rmtree(BUILD_DIR, ignore_errors=True) # copy static files for static_dir in STATIC_DIRS: shutil.copytree( os.path.join(SOURCE_DIR, static_dir), os.path.join(BUILD_DIR, static_dir) ) # build "static" pages pages = get_pages() for slug, content in pages: print(f"rendering {slug}") html = render_markdown(content, layout) save_html(slug, html) # build project pages projects = get_projects() project_links = [] for project_id, title, slug, description, image in projects: print(f"rendering {slug}") md = f"# {title}\n\n{description}\n\n" + "".join( render_update(created_at, content, attachment_urls) for created_at, content, attachment_urls in get_updates(project_id) ) html = render_markdown(md, layout) save_html(slug, html, subdirectory="projects") # project index page for project_id, title, slug, description, image in projects: project_links.append( f"""## [{title}]({slug}.html)\n\n<a href="{slug}.html"><img class="project-cover" src="{image}"/></a>\n""" ) project_list = "".join(project_links) project_index_md = f"# Projects\n\n{project_list}" project_index_html = render_markdown(project_index_md, layout) save_html("index", project_index_html, subdirectory="projects") # build log page updates = get_latest_updates(20) log_md = "# Build log\n\n" for created_at, content, attachment_urls in updates: log_md += render_update(created_at, content, attachment_urls) log_html = render_markdown(log_md, layout) save_html("index", log_html, subdirectory="build-log") >>>>>>> REPLACE ```
1 parent 5905825 commit f76061b

1 file changed

Lines changed: 47 additions & 0 deletions

File tree

build.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,53 @@ def render_update(created_at, content, attachment_urls):
8888
md += f"![attachment]({url})\n\n"
8989
return md
9090

91+
92+
def build_website(layout):
93+
shutil.rmtree(BUILD_DIR, ignore_errors=True)
94+
95+
# copy static files
96+
for static_dir in STATIC_DIRS:
97+
shutil.copytree(
98+
os.path.join(SOURCE_DIR, static_dir), os.path.join(BUILD_DIR, static_dir)
99+
)
100+
101+
# build "static" pages
102+
pages = get_pages()
103+
for slug, content in pages:
104+
print(f"rendering {slug}")
105+
html = render_markdown(content, layout)
106+
save_html(slug, html)
107+
108+
# build project pages
109+
projects = get_projects()
110+
111+
project_links = []
112+
for project_id, title, slug, description, image in projects:
113+
print(f"rendering {slug}")
114+
md = f"# {title}\n\n{description}\n\n" + "".join(
115+
render_update(created_at, content, attachment_urls)
116+
for created_at, content, attachment_urls in get_updates(project_id)
117+
)
118+
html = render_markdown(md, layout)
119+
save_html(slug, html, subdirectory="projects")
120+
121+
# project index page
122+
for project_id, title, slug, description, image in projects:
123+
project_links.append(
124+
f"""## [{title}]({slug}.html)\n\n<a href="{slug}.html"><img class="project-cover" src="{image}"/></a>\n"""
125+
)
126+
project_list = "".join(project_links)
127+
project_index_md = f"# Projects\n\n{project_list}"
128+
project_index_html = render_markdown(project_index_md, layout)
129+
save_html("index", project_index_html, subdirectory="projects")
130+
131+
# build log page
132+
updates = get_latest_updates(20)
133+
log_md = "# Build log\n\n"
134+
for created_at, content, attachment_urls in updates:
135+
log_md += render_update(created_at, content, attachment_urls)
136+
log_html = render_markdown(log_md, layout)
137+
save_html("index", log_html, subdirectory="build-log")
91138
shutil.rmtree(BUILD_DIR, ignore_errors=True)
92139

93140
# copy static files

0 commit comments

Comments
 (0)