summaryrefslogtreecommitdiffhomepage
path: root/org/gleam-ocda.org
diff options
context:
space:
mode:
Diffstat (limited to 'org/gleam-ocda.org')
-rw-r--r--org/gleam-ocda.org340
1 files changed, 171 insertions, 169 deletions
diff --git a/org/gleam-ocda.org b/org/gleam-ocda.org
index 2cb2e20..c10554e 100644
--- a/org/gleam-ocda.org
+++ b/org/gleam-ocda.org
@@ -177,6 +177,7 @@ fn fail(message: String) -> Nil {
fn halt(code: Int) -> Nil
#+END_SRC
+* gleam :gleam:
** model
*** tangle
@@ -476,8 +477,9 @@ pub fn is_preamble_block(name: String) -> Bool {
}
#+END_SRC
-** inventory
-*** tangle
+** what to load
+*** inventory
+**** tangle
#+HEADER: :tangle ../gleam/src/ocda/inventory.gleam
#+HEADER: :noweb yes
@@ -486,7 +488,7 @@ pub fn is_preamble_block(name: String) -> Bool {
<<inventory>>
#+END_SRC
-*** source
+**** source
#+NAME: inventory
#+HEADER: :no-expand
@@ -631,8 +633,9 @@ fn source_value(source: dict.Dict(String, String), key: String) -> String {
fn sha256_hex(bytes: BitArray) -> String
#+END_SRC
-** load
-*** tangle
+*** load
+**** load
+***** tangle
#+HEADER: :tangle ../gleam/src/ocda/load.gleam
#+HEADER: :noweb yes
@@ -640,8 +643,7 @@ fn sha256_hex(bytes: BitArray) -> String
////<<doc_header_including_copyright_and_license>>
<<load>>
#+END_SRC
-
-*** source
+***** source
#+NAME: load
#+HEADER: :no-expand
@@ -727,8 +729,8 @@ pub fn load(
}
#+END_SRC
-** load pod
-*** tangle
+**** load pod
+***** tangle
#+HEADER: :tangle ../gleam/src/ocda/load_pod.gleam
#+HEADER: :noweb yes
@@ -737,7 +739,7 @@ pub fn load(
<<load_pod>>
#+END_SRC
-*** source
+***** source
#+NAME: load_pod
#+HEADER: :no-expand
@@ -882,8 +884,8 @@ pub fn bit_array_to_string(b: BitArray) -> Result(String, Nil) {
}
#+END_SRC
-** load ssp
-*** tangle
+**** load ssp
+***** tangle
#+HEADER: :tangle ../gleam/src/ocda/load_ssp.gleam
#+HEADER: :noweb yes
@@ -892,7 +894,7 @@ pub fn bit_array_to_string(b: BitArray) -> Result(String, Nil) {
<<load_ssp>>
#+END_SRC
-*** source
+***** source
#+NAME: load_ssp
#+HEADER: :no-expand
@@ -1385,8 +1387,8 @@ fn numbers(value: String) -> List(Int) {
}
#+END_SRC
-** load db
-*** tangle
+**** load db
+***** tangle
#+HEADER: :tangle ../gleam/src/ocda/load_db.gleam
#+HEADER: :noweb yes
@@ -1395,7 +1397,7 @@ fn numbers(value: String) -> List(Int) {
<<load_db>>
#+END_SRC
-*** source
+***** source
#+NAME: load_db
#+HEADER: :no-expand
@@ -2071,7 +2073,159 @@ pub fn carried_of_file(path: String) -> Result(List(List(String)), String) {
}
#+END_SRC
-** zip
+*** images
+**** tangle
+
+#+HEADER: :tangle ../gleam/src/ocda/images.gleam
+#+HEADER: :noweb yes
+#+BEGIN_SRC org
+////<<doc_header_including_copyright_and_license>>
+<<images>>
+#+END_SRC
+
+**** source
+
+#+NAME: images
+#+HEADER: :no-expand
+#+BEGIN_SRC gleam
+//// An image's bytes, from whichever source the document came from.
+////
+//// The model *names* images, with digest, byte size and pixel size; it does
+//// not hold them. That is right - an abstraction of a document is not a
+//// bundle of its assets - but a writer of any format that shows images needs
+//// the bytes, so something has to know where they are. This is that
+//// something, and it is in the library rather than in a writer because the
+//// answer depends on the *input form* and not on the output.
+////
+//// | `.ocda.db` | the `files` table, role 'image', by name |
+//// | `.zip` | the archive entry whose path ends in `/image/<name>` |
+//// | `.sisupod` | the same, if the frame can be unwrapped, which on the BEAM it cannot |
+//// | `.ssp` | beside it: a pod puts the abstraction in `media/abstraction/` and its images in `media/image/` |
+
+import gleam/bit_array
+import gleam/dynamic/decode
+import gleam/list
+import gleam/result
+import gleam/string
+import ocda/load
+import ocda/zip
+import simplifile
+import sqlight
+
+/// The bytes, or an error saying why not.
+pub fn image_bytes(source: String, name: String) -> Result(BitArray, String) {
+ case load.source_of(source) {
+ load.OcdaDb -> from_db(source, name)
+ load.Pod -> from_archive(source, name)
+ load.Ssp -> from_beside(source, name)
+ load.Unknown -> Error(source <> ": not a source this library reads")
+ }
+}
+
+/// Can this source give images at all?
+///
+/// A writer asks before it decides whether to emit them, because an `<img>`
+/// pointing at a file it did not include is worse than no image.
+pub fn can_give_images(source: String) -> Bool {
+ case load.source_of(source) {
+ load.OcdaDb -> True
+ load.Pod -> zip_readable(source)
+ load.Ssp -> image_dir_beside(source) != ""
+ load.Unknown -> False
+ }
+}
+
+fn zip_readable(path: String) -> Bool {
+ case simplifile.read_bits(path) {
+ Error(_) -> False
+ Ok(bytes) ->
+ case zip.entries(bytes) {
+ Ok(_) -> True
+ Error(_) -> False
+ }
+ }
+}
+
+fn from_db(path: String, name: String) -> Result(BitArray, String) {
+ case sqlight.open("file:" <> path <> "?mode=ro") {
+ Error(e) -> Error(path <> ": " <> e.message)
+ Ok(conn) -> {
+ let out =
+ sqlight.query(
+ "select data from files where role = 'image' and name = ?1",
+ on: conn,
+ with: [sqlight.text(name)],
+ expecting: blob(),
+ )
+ |> result.replace_error(path <> ": could not read " <> name)
+ let _ = sqlight.close(conn)
+ case out {
+ Ok([data, ..]) -> Ok(data)
+ Ok([]) -> Error(name <> ": not in this database")
+ Error(e) -> Error(e)
+ }
+ }
+ }
+}
+
+/// A blob column, read as bytes rather than as text: an image read as text
+/// stops at the first zero byte, and a png's ninth byte is zero.
+fn blob() -> decode.Decoder(BitArray) {
+ use v <- decode.field(0, decode.bit_array)
+ decode.success(v)
+}
+
+fn from_archive(path: String, name: String) -> Result(BitArray, String) {
+ use bytes <- result.try(
+ simplifile.read_bits(path)
+ |> result.replace_error(path <> ": cannot read"),
+ )
+ use entries <- result.try(zip.entries(bytes))
+ case list.find(entries, fn(e) { string.ends_with(e.name, "/image/" <> name) }) {
+ Error(_) -> Error(name <> ": not in this archive")
+ Ok(entry) -> zip.read(bytes, entry)
+ }
+}
+
+fn from_beside(path: String, name: String) -> Result(BitArray, String) {
+ case image_dir_beside(path) {
+ "" -> Error(name <> ": a .ssp outside a pod has no image directory beside it")
+ dir ->
+ simplifile.read_bits(dir <> "/" <> name)
+ |> result.replace_error(dir <> "/" <> name <> ": cannot read")
+ }
+}
+
+/// `media/abstraction/<x>.ssp` -> `media/image/`
+fn image_dir_beside(path: String) -> String {
+ let parts = string.split(path, on: "/")
+ let dir = case list.reverse(parts) {
+ [_file, ..rest] -> string.join(list.reverse(rest), "/")
+ [] -> ""
+ }
+ case string.ends_with(dir, "abstraction") {
+ False -> ""
+ True -> {
+ let parent = case list.reverse(string.split(dir, on: "/")) {
+ [_, ..rest] -> string.join(list.reverse(rest), "/")
+ [] -> ""
+ }
+ let image_dir = parent <> "/image"
+ case simplifile.is_directory(image_dir) {
+ Ok(True) -> image_dir
+ _ -> ""
+ }
+ }
+ }
+}
+
+/// Unused here, kept so that a caller can size a read before making it.
+pub fn byte_size(b: BitArray) -> Int {
+ bit_array.byte_size(b)
+}
+#+END_SRC
+
+** compression - zip
*** tangle
#+HEADER: :tangle ../gleam/src/ocda/zip.gleam
@@ -2258,158 +2412,6 @@ fn text(bytes: BitArray, at: Int, len: Int) -> Result(String, String) {
}
#+END_SRC
-** imapges
-*** tangle
-
-#+HEADER: :tangle ../gleam/src/ocda/images.gleam
-#+HEADER: :noweb yes
-#+BEGIN_SRC org
-////<<doc_header_including_copyright_and_license>>
-<<images>>
-#+END_SRC
-
-*** source
-
-#+NAME: images
-#+HEADER: :no-expand
-#+BEGIN_SRC gleam
-//// An image's bytes, from whichever source the document came from.
-////
-//// The model *names* images, with digest, byte size and pixel size; it does
-//// not hold them. That is right - an abstraction of a document is not a
-//// bundle of its assets - but a writer of any format that shows images needs
-//// the bytes, so something has to know where they are. This is that
-//// something, and it is in the library rather than in a writer because the
-//// answer depends on the *input form* and not on the output.
-////
-//// | `.ocda.db` | the `files` table, role 'image', by name |
-//// | `.zip` | the archive entry whose path ends in `/image/<name>` |
-//// | `.sisupod` | the same, if the frame can be unwrapped, which on the BEAM it cannot |
-//// | `.ssp` | beside it: a pod puts the abstraction in `media/abstraction/` and its images in `media/image/` |
-
-import gleam/bit_array
-import gleam/dynamic/decode
-import gleam/list
-import gleam/result
-import gleam/string
-import ocda/load
-import ocda/zip
-import simplifile
-import sqlight
-
-/// The bytes, or an error saying why not.
-pub fn image_bytes(source: String, name: String) -> Result(BitArray, String) {
- case load.source_of(source) {
- load.OcdaDb -> from_db(source, name)
- load.Pod -> from_archive(source, name)
- load.Ssp -> from_beside(source, name)
- load.Unknown -> Error(source <> ": not a source this library reads")
- }
-}
-
-/// Can this source give images at all?
-///
-/// A writer asks before it decides whether to emit them, because an `<img>`
-/// pointing at a file it did not include is worse than no image.
-pub fn can_give_images(source: String) -> Bool {
- case load.source_of(source) {
- load.OcdaDb -> True
- load.Pod -> zip_readable(source)
- load.Ssp -> image_dir_beside(source) != ""
- load.Unknown -> False
- }
-}
-
-fn zip_readable(path: String) -> Bool {
- case simplifile.read_bits(path) {
- Error(_) -> False
- Ok(bytes) ->
- case zip.entries(bytes) {
- Ok(_) -> True
- Error(_) -> False
- }
- }
-}
-
-fn from_db(path: String, name: String) -> Result(BitArray, String) {
- case sqlight.open("file:" <> path <> "?mode=ro") {
- Error(e) -> Error(path <> ": " <> e.message)
- Ok(conn) -> {
- let out =
- sqlight.query(
- "select data from files where role = 'image' and name = ?1",
- on: conn,
- with: [sqlight.text(name)],
- expecting: blob(),
- )
- |> result.replace_error(path <> ": could not read " <> name)
- let _ = sqlight.close(conn)
- case out {
- Ok([data, ..]) -> Ok(data)
- Ok([]) -> Error(name <> ": not in this database")
- Error(e) -> Error(e)
- }
- }
- }
-}
-
-/// A blob column, read as bytes rather than as text: an image read as text
-/// stops at the first zero byte, and a png's ninth byte is zero.
-fn blob() -> decode.Decoder(BitArray) {
- use v <- decode.field(0, decode.bit_array)
- decode.success(v)
-}
-
-fn from_archive(path: String, name: String) -> Result(BitArray, String) {
- use bytes <- result.try(
- simplifile.read_bits(path)
- |> result.replace_error(path <> ": cannot read"),
- )
- use entries <- result.try(zip.entries(bytes))
- case list.find(entries, fn(e) { string.ends_with(e.name, "/image/" <> name) }) {
- Error(_) -> Error(name <> ": not in this archive")
- Ok(entry) -> zip.read(bytes, entry)
- }
-}
-
-fn from_beside(path: String, name: String) -> Result(BitArray, String) {
- case image_dir_beside(path) {
- "" -> Error(name <> ": a .ssp outside a pod has no image directory beside it")
- dir ->
- simplifile.read_bits(dir <> "/" <> name)
- |> result.replace_error(dir <> "/" <> name <> ": cannot read")
- }
-}
-
-/// `media/abstraction/<x>.ssp` -> `media/image/`
-fn image_dir_beside(path: String) -> String {
- let parts = string.split(path, on: "/")
- let dir = case list.reverse(parts) {
- [_file, ..rest] -> string.join(list.reverse(rest), "/")
- [] -> ""
- }
- case string.ends_with(dir, "abstraction") {
- False -> ""
- True -> {
- let parent = case list.reverse(string.split(dir, on: "/")) {
- [_, ..rest] -> string.join(list.reverse(rest), "/")
- [] -> ""
- }
- let image_dir = parent <> "/image"
- case simplifile.is_directory(image_dir) {
- Ok(True) -> image_dir
- _ -> ""
- }
- }
- }
-}
-
-/// Unused here, kept so that a caller can size a read before making it.
-pub fn byte_size(b: BitArray) -> Int {
- bit_array.byte_size(b)
-}
-#+END_SRC
-
** dump
*** tangle