2727import datetime
2828from collections import OrderedDict , namedtuple
2929from re import search , sub
30- from typing import Any , Callable , NamedTuple , Tuple
30+ from typing import Any , Callable , NamedTuple , Optional , Tuple
3131from urllib import parse
3232import falcon
3333import jinja2
@@ -107,7 +107,7 @@ def get_project_error_page(req, resp, exception: ElixirProjectError):
107107
108108 versions_raw = get_versions_cached (query , req .context , project )
109109 get_url_with_new_version = lambda v : stringify_source_path (project , v , '/' )
110- versions , current_version_path = get_versions (versions_raw , get_url_with_new_version , version )
110+ versions , current_version_path = get_versions (versions_raw , get_url_with_new_version , None , version )
111111
112112 template_ctx = {
113113 ** template_ctx ,
@@ -192,6 +192,10 @@ def validate_project_and_version(ctx, project, version):
192192def get_source_base_url (project : str , version : str ) -> str :
193193 return f'/{ parse .quote (project , safe = "" ) } /{ parse .quote (version , safe = "" ) } /source'
194194
195+ def stringify_diff_path (project : str , version : str , version_other : str , path : str ) -> str :
196+ return f'/{ parse .quote (project , safe = "" ) } /{ parse .quote (version , safe = "" ) } /diff/' + \
197+ f'{ parse .quote (version_other , safe = "" ) } /{ path } '
198+
195199# Converts ParsedSourcePath to a string with corresponding URL path
196200def stringify_source_path (project : str , version : str , path : str ) -> str :
197201 if not path .startswith ('/' ):
@@ -403,7 +407,7 @@ def get_projects(basedir: str) -> list[ProjectEntry]:
403407
404408# Tuple of version name and URL to chosen resource with that version
405409# Used to render version list in the sidebar
406- VersionEntry = namedtuple ('VersionEntry' , 'version, url' )
410+ VersionEntry = namedtuple ('VersionEntry' , 'version, url, diff_url ' )
407411
408412# Takes result of Query.query('version') and prepares it for the sidebar template.
409413# Returns an OrderedDict with version information and optionally a triple with
@@ -416,6 +420,7 @@ def get_projects(basedir: str) -> list[ProjectEntry]:
416420# current_version: string with currently browsed version
417421def get_versions (versions : OrderedDict [str , OrderedDict [str , str ]],
418422 get_url : Callable [[str ], str ],
423+ get_diff_url : Optional [Callable [[str ], str ]],
419424 current_version : str ) -> Tuple [dict [str , dict [str , list [VersionEntry ]]], Tuple [str | None , str | None , str | None ]]:
420425
421426 result = OrderedDict ()
@@ -427,13 +432,14 @@ def get_versions(versions: OrderedDict[str, OrderedDict[str, str]],
427432 result [major ] = OrderedDict ()
428433 if minor not in result [major ]:
429434 result [major ][minor ] = []
430- result [major ][minor ].append (VersionEntry (v , get_url (v )))
435+ result [major ][minor ].append (
436+ VersionEntry (v , get_url (v ), get_diff_url (v ) if get_diff_url is not None else None )
437+ )
431438 if v == current_version :
432439 current_version_path = (major , minor , v )
433440
434441 return result , current_version_path
435442
436- # Caches get_versions result in a context object
437443def get_versions_cached (q , ctx , project ):
438444 with ctx .versions_cache_lock :
439445 if project not in ctx .versions_cache :
@@ -452,9 +458,9 @@ def get_versions_cached(q, ctx, project):
452458# project: name of the project
453459# version: version of the project
454460def get_layout_template_context (q : Query , ctx : RequestContext , get_url_with_new_version : Callable [[str ], str ],
455- project : str , version : str ) -> dict [str , Any ]:
461+ get_diff_url : Optional [ Callable [[ str ], str ]], project : str , version : str ) -> dict [str , Any ]:
456462 versions_raw = get_versions_cached (q , ctx , project )
457- versions , current_version_path = get_versions (versions_raw , get_url_with_new_version , version )
463+ versions , current_version_path = get_versions (versions_raw , get_url_with_new_version , get_diff_url , version )
458464
459465 return {
460466 'projects' : get_projects (ctx .config .project_dir ),
@@ -731,14 +737,16 @@ def generate_source_page(ctx: RequestContext, q: Query,
731737 title_path = f'{ path_split [- 1 ] } - { "/" .join (path_split ) } - '
732738
733739 get_url_with_new_version = lambda v : stringify_source_path (project , v , path )
740+ get_diff_url = lambda v_other : stringify_diff_path (project , version , v_other , path )
734741
735742 # Create template context
736743 data = {
737- ** get_layout_template_context (q , ctx , get_url_with_new_version , project , version ),
744+ ** get_layout_template_context (q , ctx , get_url_with_new_version , get_diff_url , project , version ),
738745
739746 'title_path' : title_path ,
740747 'path' : path ,
741748 'breadcrumb_links' : breadcrumb_links ,
749+ 'diff_mode_available' : True ,
742750
743751 ** template_ctx ,
744752 }
@@ -788,19 +796,23 @@ def generate_diff_page(ctx: RequestContext, q: Query,
788796 title_path = f'{ path_split [- 1 ] } - { "/" .join (path_split ) } - '
789797
790798 get_url_with_new_version = lambda v : stringify_source_path (project , v , path )
799+ get_diff_url = lambda v_other : stringify_diff_path (project , version , v_other , path )
791800
792801 template = ctx .jinja_env .get_template ('diff.html' )
793802
794803 code , code_other = generate_diff (q , project , version , version_other , path )
795804 # Create template context
796805 data = {
797- ** get_layout_template_context (q , ctx , get_url_with_new_version , project , version ),
806+ ** get_layout_template_context (q , ctx , get_url_with_new_version , get_diff_url , project , version ),
798807
799808 'code' : code ,
800809 'code_other' : code_other ,
801810 'title_path' : title_path ,
802811 'path' : path ,
803812 'breadcrumb_links' : breadcrumb_links ,
813+ 'diff_mode_available' : True ,
814+ 'diff_checked' : True ,
815+ 'diff_exit_url' : stringify_source_path (project , version , path ),
804816 }
805817
806818 return (status , template .render (data ))
@@ -883,7 +895,7 @@ def generate_ident_page(ctx: RequestContext, q: Query,
883895 get_url_with_new_version = lambda v : stringify_ident_path (project , v , family , ident )
884896
885897 data = {
886- ** get_layout_template_context (q , ctx , get_url_with_new_version , project , version ),
898+ ** get_layout_template_context (q , ctx , get_url_with_new_version , None , project , version ),
887899
888900 'searched_ident' : ident ,
889901 'current_family' : family ,
0 commit comments