From e8b5724e3c21b04952f63513befcc323d09b508b Mon Sep 17 00:00:00 2001 From: Cuihtlauac Alvarado Date: Fri, 7 Jun 2024 09:06:21 +0200 Subject: [PATCH] Single data type definition for Cookbook (#2490) Co-authored-by: Cuihtlauac ALVARADO --- src/ocamlorg_data/data.mli | 31 +------------ src/ocamlorg_data/data_intf.ml | 38 +++++++++++++++ tool/ood-gen/lib/cookbook.ml | 84 ++++++---------------------------- 3 files changed, 52 insertions(+), 101 deletions(-) diff --git a/src/ocamlorg_data/data.mli b/src/ocamlorg_data/data.mli index e766ad86f4..31a22c59b8 100644 --- a/src/ocamlorg_data/data.mli +++ b/src/ocamlorg_data/data.mli @@ -35,36 +35,7 @@ module Code_example : sig end module Cookbook : sig - type category = { - title : string; - slug : string; - subcategories : category list; - } - - type task = { - title : string; - slug : string; - category_path : string list; - description : string option; - } - - type code_block_with_explanation = { code : string; explanation : string } - - type package = { - name : string; - tested_version : string; - used_libraries : string list; - } - - type t = { - slug : string; - filepath : string; - task : task; - packages : package list; - code_blocks : code_block_with_explanation list; - code_plaintext : string; - discussion_html : string; - } + include module type of Data_intf.Cookbook val top_categories : category list val tasks : task list diff --git a/src/ocamlorg_data/data_intf.ml b/src/ocamlorg_data/data_intf.ml index 739f969a3a..2fa939b5a4 100644 --- a/src/ocamlorg_data/data_intf.ml +++ b/src/ocamlorg_data/data_intf.ml @@ -57,6 +57,44 @@ module Book = struct [@@deriving show] end +module Cookbook = struct + type category = { + title : string; + slug : string; + subcategories : category list; + } + [@@deriving show] + + type task = { + title : string; + slug : string; + category_path : string list; + description : string option; + } + [@@deriving show] + + type code_block_with_explanation = { code : string; explanation : string } + [@@deriving show] + + type package = { + name : string; + tested_version : string; + used_libraries : string list; + } + [@@deriving of_yaml, show] + + type t = { + slug : string; + filepath : string; + task : task; + packages : package list; + code_blocks : code_block_with_explanation list; + code_plaintext : string; + discussion_html : string; + } + [@@deriving show] +end + module Outreachy = struct type project = { title : string; diff --git a/tool/ood-gen/lib/cookbook.ml b/tool/ood-gen/lib/cookbook.ml index 472bf8099c..34facb783e 100644 --- a/tool/ood-gen/lib/cookbook.ml +++ b/tool/ood-gen/lib/cookbook.ml @@ -1,3 +1,5 @@ +open Data_intf.Cookbook + type task_metadata = { title : string; slug : string; @@ -12,45 +14,14 @@ type category_metadata = { } [@@deriving of_yaml] -type category = { title : string; slug : string; subcategories : category list } -[@@deriving show { with_path = false }] - -type task = { - title : string; - slug : string; - category_path : string list; - description : string option; -} -[@@deriving show { with_path = false }] - -type code_block_with_explanation = { code : string; explanation : string } -[@@deriving show { with_path = false }] - -type package = { - name : string; - tested_version : string; - used_libraries : string list; -} -[@@deriving of_yaml, show { with_path = false }] - type metadata = { packages : package list; discussion : string option } -[@@deriving of_yaml] - -type t = { - filepath : string; - slug : string; - task : task; - packages : package list; - code_blocks : code_block_with_explanation list; - code_plaintext : string; - discussion_html : string; -} [@@deriving - stable_record ~version:metadata - ~remove: - [ slug; filepath; task; discussion_html; code_blocks; code_plaintext ] - ~add:[ discussion ], - show { with_path = false }] + of_yaml, + stable_record ~version:t + ~add: + [ slug; filepath; task; discussion_html; code_blocks; code_plaintext ] + ~remove:[ discussion ], + show] let decode (tasks : task list) (fpath, (head, body)) = let ( let* ) = Result.bind in @@ -106,7 +77,7 @@ let decode (tasks : task list) (fpath, (head, body)) = let discussion_html = metadata.discussion |> Option.value ~default:"" |> render_markdown in - of_metadata ~slug ~filepath:fpath ~task ~discussion_html ~code_blocks + metadata_to_t ~slug ~filepath:fpath ~task ~discussion_html ~code_blocks ~code_plaintext:body metadata) metadata |> Result.map_error (Utils.where fpath) @@ -147,46 +118,17 @@ let tasks, top_categories = all_categories_and_tasks () let all () = Utils.map_files (decode tasks) "cookbook/*/*.ml" - |> List.sort (fun a b -> String.compare b.slug a.slug) + |> List.sort (fun (a : t) (b : t) -> String.compare b.slug a.slug) |> List.rev let template () = Format.asprintf - {| -type category = - { title : string - ; slug : string - ; subcategories : category list - } -type task = - { title : string - ; slug : string - ; category_path : string list - ; description : string option - } -type package = - { name : string - ; tested_version : string - ; used_libraries : string list - } -type code_block_with_explanation = - { code : string - ; explanation : string - } -type t = - { slug: string - ; filepath: string - ; task : task - ; packages : package list - ; code_blocks : code_block_with_explanation list - ; code_plaintext : string - ; discussion_html : string - } - + {ocaml| +include Data_intf.Cookbook let top_categories = %a let tasks = %a let all = %a -|} +|ocaml} (Fmt.brackets (Fmt.list pp_category ~sep:Fmt.semi)) top_categories (Fmt.brackets (Fmt.list pp_task ~sep:Fmt.semi))