@@ -10,31 +10,50 @@ defmodule CodeComparison.Languages do
1010 @ spec get_languages_by_topic ( String . t ( ) ) :: list ( % Language { } )
1111 def get_languages_by_topic ( topic ) do
1212 topic_dir = Path . join ( base_topics_path ( ) , topic )
13+
1314 case File . ls ( topic_dir ) do
1415 { :ok , files } ->
1516 files
1617 |> Enum . map ( & ( String . split ( & 1 , "." ) |> List . first ( ) ) )
17- |> Enum . map ( & language_build ( & 1 , topic ) ) # language_build handles its own file reads using new path logic
18+ # language_build handles its own file reads using new path logic
19+ |> Enum . map ( & language_build ( & 1 , topic ) )
1820 |> Enum . sort_by ( & & 1 . name )
21+
1922 { :error , reason } ->
20- IO . inspect ( "Failed to list languages for topic '#{ topic } ' at '#{ topic_dir } ': #{ inspect ( reason ) } " , label: "Languages Error" )
23+ IO . inspect (
24+ "Failed to list languages for topic '#{ topic } ' at '#{ topic_dir } ': #{ inspect ( reason ) } " ,
25+ label: "Languages Error"
26+ )
27+
2128 [ ]
2229 end
2330 end
2431
2532 @ spec get_language_code ( String . t ( ) , String . t ( ) ) :: String . t ( )
2633 def get_language_code ( language_name , topic ) do
27- filename = get_filename ( language_name , topic ) # This will use the updated get_filename
34+ # This will use the updated get_filename
35+ filename = get_filename ( language_name , topic )
2836
2937 if filename == nil do
30- IO . inspect ( "Filename not found for language '#{ language_name } ' in topic '#{ topic } ' when getting code." , label: "Languages Error" )
38+ IO . inspect (
39+ "Filename not found for language '#{ language_name } ' in topic '#{ topic } ' when getting code." ,
40+ label: "Languages Error"
41+ )
42+
3143 ""
3244 else
3345 file_path = Path . join ( [ base_topics_path ( ) , topic , filename ] )
46+
3447 case File . read ( file_path ) do
35- { :ok , code } -> code
48+ { :ok , code } ->
49+ code
50+
3651 { :error , reason } ->
37- IO . inspect ( "Failed to read language code for '#{ language_name } ' from '#{ file_path } ': #{ inspect ( reason ) } " , label: "Languages Error" )
52+ IO . inspect (
53+ "Failed to read language code for '#{ language_name } ' from '#{ file_path } ': #{ inspect ( reason ) } " ,
54+ label: "Languages Error"
55+ )
56+
3857 ""
3958 end
4059 end
@@ -43,20 +62,32 @@ defmodule CodeComparison.Languages do
4362 @ spec get_filename ( String . t ( ) , String . t ( ) ) :: String . t ( ) | nil
4463 defp get_filename ( language_name , topic ) do
4564 topic_dir = Path . join ( base_topics_path ( ) , topic )
65+
4666 case File . ls ( topic_dir ) do
4767 { :ok , files } ->
4868 Enum . find ( files , & ( String . split ( & 1 , "." ) |> List . first ( ) == language_name ) )
69+
4970 { :error , reason } ->
50- IO . inspect ( "Failed to list files in '#{ topic_dir } ' to find filename for '#{ language_name } ': #{ inspect ( reason ) } " , label: "Languages Error" )
71+ IO . inspect (
72+ "Failed to list files in '#{ topic_dir } ' to find filename for '#{ language_name } ': #{ inspect ( reason ) } " ,
73+ label: "Languages Error"
74+ )
75+
5176 nil
5277 end
5378 end
5479
5580 @ spec get_language ( list ( % Language { } ) , String . t ( ) ) :: % Language { }
5681 def get_language ( topic_languages , current_language_name ) do
5782 if topic_languages == [ ] do
58- % Language { # Using alias for brevity
59- name: "" , code: "" , topic: "" , commiter_name: "" , commiter_url: "" , path: ""
83+ # Using alias for brevity
84+ % Language {
85+ name: "" ,
86+ code: "" ,
87+ topic: "" ,
88+ commiter_name: "" ,
89+ commiter_url: "" ,
90+ path: ""
6091 }
6192 else
6293 # The topic should ideally be consistent for all languages in topic_languages.
@@ -67,14 +98,17 @@ defmodule CodeComparison.Languages do
6798 found_language_struct = Enum . find ( topic_languages , & ( & 1 . name == current_language_name ) )
6899
69100 cond do
70- found_language_struct -> # Language found in the pre-loaded list
101+ # Language found in the pre-loaded list
102+ found_language_struct ->
71103 found_language_struct |> put_commit_values ( )
72- true -> # Language not found by name, or current_language_name is empty. Default to first.
73- # This path also handles if current_language_name was for a language not in this topic.
74- # The original code's `Enum.member?` check followed by `language_build` or `List.first`
75- # had a subtle difference. `language_build` would reload the code.
76- # Here, we assume `topic_languages` are complete structs.
77- # If `current_language_name` isn't in the list, we take the first.
104+
105+ # Language not found by name, or current_language_name is empty. Default to first.
106+ true ->
107+ # This path also handles if current_language_name was for a language not in this topic.
108+ # The original code's `Enum.member?` check followed by `language_build` or `List.first`
109+ # had a subtle difference. `language_build` would reload the code.
110+ # Here, we assume `topic_languages` are complete structs.
111+ # If `current_language_name` isn't in the list, we take the first.
78112 topic_languages |> List . first ( ) |> put_commit_values ( )
79113 end
80114 end
@@ -84,7 +118,8 @@ defmodule CodeComparison.Languages do
84118 def get_language_by_topic ( topic , current_language_name ) do
85119 # This function, as per existing code, rebuilds the language struct.
86120 # It's used in HomeLive mount and language change event.
87- language_build ( current_language_name , topic ) # This now uses new path logic internally
121+ # This now uses new path logic internally
122+ language_build ( current_language_name , topic )
88123 |> put_commit_values ( )
89124 end
90125
@@ -96,15 +131,22 @@ defmodule CodeComparison.Languages do
96131 if filename do
97132 "topics/#{ topic } /#{ filename } "
98133 else
99- IO . inspect ( "Filename is nil for language '#{ language_name } ' in topic '#{ topic } ' during language_build. Path will be empty." , label: "Languages Warning" )
100- "" # Empty path if filename couldn't be determined
134+ IO . inspect (
135+ "Filename is nil for language '#{ language_name } ' in topic '#{ topic } ' during language_build. Path will be empty." ,
136+ label: "Languages Warning"
137+ )
138+
139+ # Empty path if filename couldn't be determined
140+ ""
101141 end
102142
103143 Language . build ( % {
104144 name: language_name ,
105- code: get_language_code ( language_name , topic ) , # Uses new path logic and error handling
145+ # Uses new path logic and error handling
146+ code: get_language_code ( language_name , topic ) ,
106147 topic: topic ,
107- path: String . replace ( github_path , " " , "%20" ) # Path for GitHub API
148+ # Path for GitHub API
149+ path: String . replace ( github_path , " " , "%20" )
108150 } )
109151 end
110152
@@ -114,20 +156,25 @@ defmodule CodeComparison.Languages do
114156 # Ensure path is not empty before calling GitHub API
115157 if language . path && language . path != "" do
116158 case Application . get_env ( :code_comparison , :github ) [ :token ] do
117- nil -> put_empty_commit_values ( language )
118- _ -> # Pass the actual language struct, not just path
119- put_commit_values_with_api ( language , Github . get_last_commit ( language . path ) )
159+ nil ->
160+ put_empty_commit_values ( language )
161+
162+ # Pass the actual language struct, not just path
163+ _ ->
164+ put_commit_values_with_api ( language , Github . get_last_commit ( language . path ) )
120165 end
121166 else
122- put_empty_commit_values ( language ) # Path is empty, skip GitHub call
167+ # Path is empty, skip GitHub call
168+ put_empty_commit_values ( language )
123169 end
124170 end
125171
126172 # Renamed to avoid clash and clarify it's the one calling API
127173 defp put_commit_values_with_api ( language , { :ok , body } ) do
128174 # Assuming body structure is a list of commits
129- first_commit = List . first ( body ) # Safe navigation for author needed
130-
175+ # Safe navigation for author needed
176+ first_commit = List . first ( body )
177+
131178 author_name = first_commit |> get_in ( [ "author" , "login" ] )
132179 author_url = first_commit |> get_in ( [ "author" , "html_url" ] )
133180
0 commit comments