From 4959189b32af60bea681f08f5f5ab697bf155c77 Mon Sep 17 00:00:00 2001 From: mous1985 Date: Fri, 10 Jan 2025 19:26:30 +0100 Subject: [PATCH] ref: func writeRecipe to method (r Recipe)Render func writeProfile to method (p Profile)Render --- examples/gno.land/r/mouss/home/home.gno | 83 ++++++++++++++----------- 1 file changed, 46 insertions(+), 37 deletions(-) diff --git a/examples/gno.land/r/mouss/home/home.gno b/examples/gno.land/r/mouss/home/home.gno index e677c3318c1..5f8fbb8732c 100644 --- a/examples/gno.land/r/mouss/home/home.gno +++ b/examples/gno.land/r/mouss/home/home.gno @@ -7,6 +7,7 @@ import ( "gno.land/p/demo/mux" "gno.land/p/demo/ufmt" + "gno.land/p/moul/addrset" "gno.land/p/moul/md" "gno.land/r/mouss/config" @@ -44,7 +45,7 @@ var ( Email: "mustapha.benazzouz@outlook.fr", Github: "https://github.com/mous1985", LinkedIn: "https://www.linkedin.com/in/mustapha-benazzouz-88646887/", - Followers: make([]std.Address, 0), + Followers: addrset.Set{}, } router = mux.NewRouter() recipes []*Recipe @@ -52,8 +53,8 @@ var ( Name: "Authentic Margherita Pizza 🤌", Origin: "Naples, 🇮🇹", Author: config.Address(), - Ingredients: " \n\n- 1kg 00 flour\n\n- 500ml water\n\n- 3g fresh yeast\n\n- 20g sea salt\n\n- San Marzano tomatoes\n\n- Fresh buffalo mozzarella\n\n- Fresh basil\n\n- Extra virgin olive oil", - Instructions: "\n\n1. Mix flour and water until incorporated\n\n2. Add yeast and salt, knead for 20 minutes\n\n3. Let rise for 2 hours at room temperature\n\n4. Divide into 250g balls\n\n5. Cold ferment for 24-48 hours\n\n6. Shape by hand, being gentle with the dough\n\n7. Top with crushed tomatoes, torn mozzarella, and basil\n\n8. Cook at 450°C for 60-90 seconds", + Ingredients: " 1kg 00 flour\n 500ml water\n 3g fresh yeast\n 20g sea salt\n San Marzano tomatoes\n Fresh buffalo mozzarella\n Fresh basil\n Extra virgin olive oil", + Instructions: " Mix flour and water until incorporated\n Add yeast and salt, knead for 20 minutes\n Let rise for 2 hours at room temperature\n Divide into 250g balls\n Cold ferment for 24-48 hours\n Shape by hand, being gentle with the dough\n Top with crushed tomatoes, torn mozzarella, and basil\n Cook at 450°C for 60-90 seconds", Tips: "Use a pizza steel or stone preheated for at least 1 hour. The dough should be soft and extensible. For best results, cook in a wood-fired oven.", } ) @@ -101,8 +102,8 @@ func validateRecipe(name, ingredients, instructions string) error { // If the caller is not authorized, it returns an error. // If the address is already being followed, it returns an error. // Otherwise, it adds the address to the list of followers and returns nil. -//TODO:any user can follow and to be followed by any other user -//TODO: add a function to unfollow +// TODO:any user can follow and to be followed by any other user +// TODO: add a function to unfollow func Follow(addr std.Address) error { caller := std.PrevRealm().Addr() @@ -112,12 +113,12 @@ func Follow(addr std.Address) error { if profile.Followers.Has(addr) { return ufmt.Errorf("address %s is already following", addr) } - profile.Followers.Add(addr) + profile.Followers.Add(addr) //can't add the same address twice return nil } func isUser(addr std.Address) bool { - return !isAuthorized(addr) && !profile.Followers.Has(addr) + return !isAuthorized(addr) } func isAuthorized(addr std.Address) bool { return addr == config.Address() || addr == config.Backup() @@ -126,7 +127,8 @@ func isAuthorized(addr std.Address) bool { func renderRecipes(res *mux.ResponseWriter, req *mux.Request) { var b strings.Builder b.WriteString("## World Kitchen\n\n------\n\n") - writeRecipe(&b, margheritaPizza) + + b.WriteString(margheritaPizza.Render()) if len(recipes) == 0 { b.WriteString("No recipes yet. Be the first to add one!\n") @@ -135,45 +137,52 @@ func renderRecipes(res *mux.ResponseWriter, req *mux.Request) { } for _, recipe := range recipes { - writeRecipe(&b, recipe) + b.WriteString(recipe.Render()) } + res.Write(b.String()) } -func writeRecipe(b *strings.Builder, recipe *Recipe) { - b.WriteString(md.H2(recipe.Name)) - b.WriteString(md.Bold("Author: ") + recipe.Author.String() + "\n\n") - b.WriteString(md.Bold("Origin: ") + recipe.Origin + "\n\n") - b.WriteString(md.Bold("Ingredients:") + "\n" + md.BulletList(strings.Split(recipe.Ingredients, "\n")) + "\n") - b.WriteString(md.Bold("Instructions:") + "\n" + md.OrderedList(strings.Split(recipe.Instructions, "\n")) + "\n") - - if recipe.Tips != "" { - b.WriteString(md.Italic("💡 Tips: "+recipe.Tips) + "\n\n") +func (r Recipe) Render() string { + var b strings.Builder + b.WriteString(md.H2(r.Name)) + b.WriteString(md.Bold("Author:") + "\n" + r.Author.String() + "\n\n") + b.WriteString(md.Bold("Origin:") + "\n" + r.Origin + "\n\n") + b.WriteString(md.Bold("Ingredients:") + "\n" + md.BulletList(strings.Split(r.Ingredients, "\n")) + "\n\n") + b.WriteString(md.Bold("Instructions:") + "\n" + md.OrderedList(strings.Split(r.Instructions, "\n")) + "\n\n") + + if r.Tips != "" { + b.WriteString(md.Italic("💡 Tips:"+"\n"+r.Tips) + "\n\n") } b.WriteString(md.HorizontalRule() + "\n") + return b.String() } func renderHomepage(res *mux.ResponseWriter, req *mux.Request) { var b strings.Builder writeNavigation(&b) - writeProfile(&b) + b.WriteString(profile.Render()) res.Write(b.String()) } -func writeProfile(b *strings.Builder) { +func (p Profile) Render() string { + var b strings.Builder + b.WriteString(md.H1("Welcome to my Homepage") + "\n\n" + md.HorizontalRule() + "\n\n") - writeGnoArt(b) + writeGnoArt(&b) b.WriteString(md.HorizontalRule() + "\n\n" + md.H2("About Me") + "\n\n") - b.WriteString(md.Image("avatar", profile.Avatar) + "\n\n") - b.WriteString(profile.AboutMe + "\n\n" + md.HorizontalRule() + "\n\n") + b.WriteString(md.Image("avatar", p.Avatar) + "\n\n") + b.WriteString(p.AboutMe + "\n\n" + md.HorizontalRule() + "\n\n") b.WriteString(md.H3("Contact") + "\n\n") b.WriteString(md.BulletList([]string{ - "Email: " + profile.Email, - "GitHub: " + md.Link("@mous1985", profile.Github), - "LinkedIn: " + md.Link("Mustapha", profile.LinkedIn), + "Email: " + p.Email, + "GitHub: " + md.Link("@mous1985", p.Github), + "LinkedIn: " + md.Link("Mustapha", p.LinkedIn), })) - b.WriteString("\n\n" + md.Bold("👤 Followers: ") + strconv.Itoa(profile.Followers.Size())) + b.WriteString("\n\n" + md.Bold("👤 Followers: ") + strconv.Itoa(p.Followers.Size())) + + return b.String() } func writeNavigation(b *strings.Builder) { navItems := []string{ @@ -189,16 +198,16 @@ func Render(path string) string { func writeGnoArt(b *strings.Builder) { b.WriteString("```\n") for _, line := range []string{ - " -==++. ", - " *@@@@= @- -@", - " #@@@@@: -==-.-- :-::===: .-++-. @- .===:.- .-.-==- .===:=@", - " #@@@@@@@: -@@%**%@@ #@@#*#@@- *@@**@@* @- +%=::-*@ +@=-:-@* +%=::-*@", - " +@%#**#%@@ %@+ :@@ *@+ #@=+@% %@+ @= :@: -@ +% +%.@: -@", - " -: - *@%:..+@@ *@+ #@=-@@: :@@= @- .@= =@ +@ *%.@= =@", - " --:==+=-:=. =%@%#*@@ *@+ #@+ =%@%%@%= #* %#=.:%*===*@ +% +% -%*===*@", - " -++++=++++. =-:::*@# . . .::. .. :: .:: . . .:: .", - " .-=+++=: .*###%#= ", - " :: ", + " -==++. ", + " *@@@@= @- -@", + " #@@@@@: -==-.-- :-::===: .-++-. @- .===:.- .-.-==- .===:=@", + " #@@@@@@@: -@@%**%@@ #@@#*#@@- *@@**@@* @- +%=::-*@ +@=-:-@* +%=::-*@", + " +@%#**#%@@ %@+ :@@ *@+ #@=+@% %@+ @= :@: -@ +% +%.@: -@", + " -: - *@%:..+@@ *@+ #@=-@@: :@@= @- .@= =@ +@ *%.@= =@", + " --:==+=-:=. =%@%#*@@ *@+ #@+ =%@%%@%= #* %#=.:%*===*@ +% +% -%*===*@", + " -++++=++++. =-:::*@# . . .::. .. :: .:: . . .:: .", + " .-=+++=: .*###%#= ", + " :: ", } { b.WriteString(line + "\n") }