require "kemal" require "json" require "markd" # Structure to hold recipe metadata struct Recipe property tags : Array(String) property file_path : String property html_content : String # explicit initializer to ensure all properties are set def initialize(tags : Array(String), file_path : String, html_content : String) @tags = tags @file_path = file_path @html_content = html_content end def self.load_from_folder(folder_path : String) recipes = [] of Recipe Dir.glob("#{folder_path}/*/*.md") do |file_path| content = File.read(file_path) # Extract front matter for tags only tags = [] of String body_content = content if content.starts_with?("---") front_matter_end = content.index(/---\s*\n/, 4) # find the second "---" line if front_matter_end front_matter = content[4...front_matter_end].strip body_content = content[(front_matter_end + 4)..-1].strip # Extract tags if present if match = front_matter.match(/tags:\s*\[(.*?)\]/) tags = match[1].split(",").map(&.strip) end end end # Render the markdown content without the tags html_content = Markd.to_html(body_content) # Append tags to the end in a readable format if !tags.empty? html_content += "
" end recipes << Recipe.new(tags, file_path, html_content) end recipes end end # Load recipes from the recipes directory recipes = Recipe.load_from_folder("recipes") # Route to display all recipes by category get "/" do categorized_recipes = recipes.group_by { |r| File.basename(File.dirname(r.file_path)).to_s } render "src/views/index.ecr", "src/views/layout.ecr" end # Route to display a specific recipe get "/recipes/:category/:recipe" do |env| category = env.params.url["category"] recipe = env.params.url["recipe"] recipe_path = "recipes/#{category}/#{recipe}.md" if File.exists?(recipe_path) markdown_content = File.read(recipe_path) html_content = Markd.to_html(markdown_content) render "src/views/recipe.ecr", "src/views/layout.ecr" else env.response.status_code = 404 "Recipe not found" end end # Route to display the search page get "/search" do |env| render "src/views/search.ecr", "src/views/layout.ecr" end # Route to handle search results by tag get "/search/results" do |env| query = env.params.query["tag"] || "" matched_recipes = recipes.select { |recipe| recipe.tags.includes?(query.downcase) } render "src/views/search_results.ecr", "src/views/layout.ecr" end # Start Kemal Kemal.run