22
33require "rexml/document"
44require "fileutils"
5- require "yaml"
65
76# AddonProcessor processes external add-on repositories (openhab-addons and openhab-webui).
87#
@@ -50,7 +49,7 @@ def self.process_addon_type(features, dest_addons_dir, images_dir, src_bundles_d
5049 end
5150
5251 Dir . foreach ( src_bundles_dir ) do |addon_name |
53- next if [ "." , ".." ] . include? ( addon_name )
52+ next if addon_name == "." || addon_name == ".."
5453
5554 # Match the type in package name structure (org.openhab.<type>...)
5655 parts = addon_name . split ( "." )
@@ -85,7 +84,7 @@ def self.process_addon_type(features, dest_addons_dir, images_dir, src_bundles_d
8584 FileUtils . mkdir_p ( target_dir )
8685
8786 # Copy doc/, cfg/, images/, and icons/ if they exist
88- %w[ doc cfg images icons ] . each do |sub_dir |
87+ [ " doc" , " cfg" , " images" , " icons" ] . each do |sub_dir |
8988 source_sub = File . join ( addon_path , sub_dir )
9089 if id == "zwave" && sub_dir == "doc"
9190 zwave_things_md = File . join ( source_sub , "thing.md" )
@@ -96,7 +95,9 @@ def self.process_addon_type(features, dest_addons_dir, images_dir, src_bundles_d
9695 end
9796 next
9897 end
99- FileUtils . cp_r ( source_sub , target_dir ) if Dir . exist? ( source_sub )
98+ if Dir . exist? ( source_sub )
99+ FileUtils . cp_r ( source_sub , target_dir )
100+ end
100101 end
101102
102103 # Look for README.md (case-insensitive)
@@ -108,17 +109,16 @@ def self.process_addon_type(features, dest_addons_dir, images_dir, src_bundles_d
108109
109110 # Read and parse readme
110111 readme_text = File . read ( readme_src )
111- existing_front_matter , readme_text = split_front_matter ( readme_text )
112112
113113 # Determine label from first level 1 header
114114 label = nil
115115 readme_text . each_line do |line |
116- next unless line . start_with? ( "#" )
117-
118- label = line . gsub ( "#" , "" ) . strip
119- lblremoves . each { | remove | label = label . gsub ( Regexp . new ( remove ) , "" ) }
120- label = label . strip
121- break
116+ if line . start_with? ( "#" )
117+ label = line . gsub ( "#" , "" ) . strip
118+ lblremoves . each { | remove | label = label . gsub ( Regexp . new ( remove ) , "" ) }
119+ label = label . strip
120+ break
121+ end
122122 end
123123
124124 if label . nil? || label . empty?
@@ -143,7 +143,7 @@ def self.process_addon_type(features, dest_addons_dir, images_dir, src_bundles_d
143143 end
144144
145145 # Build frontmatter hash
146- front_matter = {
146+ front = {
147147 "id" => id ,
148148 "label" => label ,
149149 "title" => "#{ label } #{ suffix } " ,
@@ -152,9 +152,9 @@ def self.process_addon_type(features, dest_addons_dir, images_dir, src_bundles_d
152152 }
153153
154154 if logo_svg
155- front_matter [ "logo" ] = "images/addons/#{ id } .svg"
155+ front [ "logo" ] = "images/addons/#{ id } .svg"
156156 elsif logo_png
157- front_matter [ "logo" ] = "images/addons/#{ id } .png"
157+ front [ "logo" ] = "images/addons/#{ id } .png"
158158 end
159159
160160 # Find install type from features list
@@ -165,25 +165,20 @@ def self.process_addon_type(features, dest_addons_dir, images_dir, src_bundles_d
165165 end
166166
167167 install_attrs = feature_entry ? feature_entry [ 1 ] : { "install" => "manual" }
168- front_matter . merge! ( install_attrs )
169- # Existing front matter from the readme takes precedence if it exists
170- front_matter . merge! ( existing_front_matter ) if existing_front_matter . is_a? ( Hash )
168+ front . merge! ( install_attrs )
169+
170+ # Build frontmatter block
171+ frontmatter_str = "---\n " + front . map { |k , v | "#{ k } : #{ v } " } . join ( "\n " ) + "\n ---\n \n "
171172
172173 # Re-format readme content: remove first heading and replace with custom template
173174 first_h1_match = readme_text . match ( /^# .*/ )
174175 heading = first_h1_match ? first_h1_match [ 0 ] : "# #{ label } "
175176 text_without_heading = first_h1_match ? readme_text . sub ( heading , "" ) : readme_text
176177
177178 addon_logo_tag = ( logo_svg || logo_png ) ? "\n \n <AddonLogo />" : ""
178- # to_yaml adds "---\n" at the start, we want to remove that and add our own "---" at the start and end
179- front_matter_str = front_matter . to_yaml . sub ( /\A ---\s *\n / , "" ) . strip
180179
181180 final_content = <<~MARKDOWN
182- ---
183- #{ front_matter_str }
184- ---
185-
186- <!-- Attention authors: Do not edit directly. Please add your changes to the appropriate source repository -->
181+ #{ frontmatter_str } <!-- Attention authors: Do not edit directly. Please add your changes to the appropriate source repository -->
187182
188183 #{ heading } #{ addon_logo_tag } #{ text_without_heading }
189184 MARKDOWN
@@ -197,23 +192,6 @@ def self.process_addon_type(features, dest_addons_dir, images_dir, src_bundles_d
197192 puts " ✔ Processed add-on type: #{ type } -> #{ dest_folder_name } "
198193 end
199194
200- def self . split_front_matter ( content )
201- match = content . match ( /\A ---\s *\n (?<front_matter>.*?)^---\s *$\n ?(?<content>.*)/m )
202-
203- if match
204- front_matter_str = match [ :front_matter ]
205- begin
206- front_matter = YAML . safe_load ( front_matter_str ) || { }
207- rescue Psych ::Exception => e
208- warn " ⚠️ Failed to parse front matter YAML: #{ e . message } "
209- front_matter = { }
210- end
211- [ front_matter , match [ :content ] ]
212- else
213- [ nil , content ]
214- end
215- end
216-
217195 def self . process_all ( distro_features_path , snapshot_features_path , addons_bundles_dir , webui_bundles_dir , dest_addons_dir , images_dir )
218196 features = collect_features ( distro_features_path , snapshot_features_path )
219197
0 commit comments