Skip to content

Commit

Permalink
Added extra api reference stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
dalmijn committed Nov 6, 2023
1 parent 69fac5b commit 0dc49d1
Show file tree
Hide file tree
Showing 14 changed files with 398 additions and 70 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ jobs:
pip install -e .
- name: Generate API Reference
run: python docs/api.py
run: |
python docs/api.py
quartodoc interlinks docs/_quarto.yml
- name: Generate docs
run: |
Expand Down
3 changes: 2 additions & 1 deletion docs/.gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/.quarto/
/_site/
/_freeze/
/_inv/
/_site/
/api/*
!api/.gitkeep
*.jupyter_cache/
Expand Down
3 changes: 3 additions & 0 deletions docs/_extensions/machow/interlinks/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
*.html
*.pdf
*_files/
7 changes: 7 additions & 0 deletions docs/_extensions/machow/interlinks/_extension.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
title: Interlinks
author: Michael Chow
version: 1.1.0
quarto-required: ">=1.2.0"
contributes:
filters:
- interlinks.lua
254 changes: 254 additions & 0 deletions docs/_extensions/machow/interlinks/interlinks.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,254 @@
local function read_inv_text(filename)
-- read file
local file = io.open(filename, "r")
if file == nil then
return nil
end
local str = file:read("a")
file:close()


local project = str:match("# Project: (%S+)")
local version = str:match("# Version: (%S+)")

local data = {project = project, version = version, items = {}}

local ptn_data =
"^" ..
"(.-)%s+" .. -- name
"([%S:]-):" .. -- domain
"([%S]+)%s+" .. -- role
"(%-?%d+)%s+" .. -- priority
"(%S*)%s+" .. -- uri
"(.-)\r?$" -- dispname


-- Iterate through each line in the file content
for line in str:gmatch("[^\r\n]+") do
if not line:match("^#") then
-- Match each line against the pattern
local name, domain, role, priority, uri, dispName = line:match(ptn_data)

-- if name is nil, raise an error
if name == nil then
error("Error parsing line: " .. line)
end

data.items[#data.items + 1] = {
name = name,
domain = domain,
role = role,
priority = priority,
uri = uri,
dispName = dispName
}
end
end
return data
end

local function read_json(filename)

local file = io.open(filename, "r")
if file == nil then
return nil
end
local str = file:read("a")
file:close()

local decoded = quarto.json.decode(str)
return decoded
end

local function read_inv_text_or_json(base_name)
local file = io.open(base_name .. ".txt", "r")
if file then
-- TODO: refactors so we don't just close the file immediately
io.close(file)
json = read_inv_text(base_name .. ".txt")

else
json = read_json(base_name .. ".json")
end

return json
end

local inventory = {}

local function lookup(search_object)

local results = {}
for _, inv in ipairs(inventory) do
for _, item in ipairs(inv.items) do
-- e.g. :external+<inv_name>:<domain>:<role>:`<name>`
if item.inv_name and item.inv_name ~= search_object.inv_name then
goto continue
end

if item.name ~= search_object.name then
goto continue
end

if search_object.role and item.role ~= search_object.role then
goto continue
end

if search_object.domain and item.domain ~= search_object.domain then
goto continue
else
if search_object.domain or item.domain == "py" then
table.insert(results, item)
end

goto continue
end

::continue::
end
end

if #results == 1 then
return results[1]
end
if #results > 1 then
quarto.log.warning("Found multiple matches for " .. search_object.name .. ", using the first match.")
return results[1]
end
if #results == 0 then
quarto.log.warning("Found no matches for object:\n", search_object)
end

return nil
end

local function mysplit (inputstr, sep)
if sep == nil then
sep = "%s"
end
local t={}
for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
table.insert(t, str)
end
return t
end

local function normalize_role(role)
if role == "func" then
return "function"
end
return role
end

local function build_search_object(str)
local starts_with_colon = str:sub(1, 1) == ":"
local search = {}
if starts_with_colon then
local t = mysplit(str, ":")
if #t == 2 then
-- e.g. :py:func:`my_func`
search.role = normalize_role(t[1])
search.name = t[2]:match("%%60(.*)%%60")
elseif #t == 3 then
-- e.g. :py:func:`my_func`
search.domain = t[1]
search.role = normalize_role(t[2])
search.name = t[3]:match("%%60(.*)%%60")
elseif #t == 4 then
-- e.g. :ext+inv:py:func:`my_func`
search.external = true

search.inv_name = t[1]:match("external%+(.*)")
search.domain = t[2]
search.role = normalize_role(t[3])
search.name = t[4]:match("%%60(.*)%%60")
else
quarto.log.warning("couldn't parse this link: " .. str)
return {}
end
else
search.name = str:match("%%60(.*)%%60")
end

if search.name == nil then
quarto.log.warning("couldn't parse this link: " .. str)
return {}
end

if search.name:sub(1, 1) == "~" then
search.shortened = true
search.name = search.name:sub(2, -1)
end
return search
end

local function report_broken_link(link, search_object, replacement)
-- TODO: how to unescape html elements like [?
return pandoc.Code(pandoc.utils.stringify(link.content))
end

function Link(link)
-- do not process regular links ----
if not link.target:match("%%60") then
return link
end

-- lookup item ----
local search = build_search_object(link.target)
local item = lookup(search)

-- determine replacement, used if no link text specified ----
local original_text = pandoc.utils.stringify(link.content)
local replacement = search.name
if search.shortened then
local t = mysplit(search.name, ".")
replacement = t[#t]
end

-- set link text ----
if original_text == "" and replacement ~= nil then
link.content = pandoc.Code(replacement)
end

-- report broken links ----
if item == nil then
return report_broken_link(link, search)
end
link.target = item.uri:gsub("%$$", search.name)


return link
end

local function fixup_json(json, prefix)
for _, item in ipairs(json.items) do
item.uri = prefix .. item.uri
end
table.insert(inventory, json)
end

return {
{
Meta = function(meta)
local json
local prefix
if meta.interlinks and meta.interlinks.sources then
for k, v in pairs(meta.interlinks.sources) do
local base_name = quarto.project.offset .. "/_inv/" .. k .. "_objects"
json = read_inv_text_or_json(base_name)
prefix = pandoc.utils.stringify(v.url)
if json ~= nil then
fixup_json(json, prefix)
end
end
end
json = read_inv_text_or_json(quarto.project.offset .. "/objects")
if json ~= nil then
fixup_json(json, "/")
end
end
},
{
Link = Link
}
}
34 changes: 20 additions & 14 deletions docs/_quarto.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,23 @@ project:
- "*.ipynb"
- "!PDF_Documentation.qmd"

filters:
- include-code-files
- interlinks
# - section-bibliographies

interlinks:
sources:
python:
url: https://docs.python.org/3/
numpy:
url: https://numpy.org/doc/stable/

metadata-files:
- api/_sidebar.yml

number-sections: false

website:
title: "Delft FIAT"
favicon: _static/logo.png
Expand Down Expand Up @@ -87,25 +104,17 @@ format:
# bibliography: references.bib
# citeproc: false

number-sections: false

filters:
- include-code-files
# - section-bibliographies

execute:
freeze: auto
cache: true

metadata-files:
- api/_sidebar.yml

quartodoc:
parser: numpy
package: fiat
title: API Reference
style: pkgdown
dir: api
render_interlinks: true
sidebar: api/_sidebar.yml
options:
include_imports: true
Expand All @@ -114,7 +123,6 @@ quartodoc:
# Basics of FIAT
sections:
- title: Basics
desc: The higher level components of FIAT.
- subtitle: Core
desc: Two main components of working with FIAT.
package: fiat
Expand All @@ -134,7 +142,6 @@ quartodoc:

# Geospatial methods
- title: GIS
desc: Geospatial bound operations.
- subtitle: Geometry (vector)
desc: Geometry specific methods
package: fiat.gis.geom
Expand Down Expand Up @@ -162,16 +169,15 @@ quartodoc:

# I/O module
- title: I/O
desc: Input/ output bound operations.
- subtitle: Methods
desc: Input/ output methods
desc: Input/ output bound operations
package: fiat.io
contents:
- open_csv
- open_geom
- open_grid
- subtitle: Objects
desc:
desc: Objects constructed from data
package: fiat.io
contents:
- name: GeomSource
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ line-length = 88
# enable pydocstyle (E), pyflake (F) and isort (I), pytest-style (PT)
select = ["E", "F", "I", "PT", "D"]
ignore-init-module-imports = true
ignore = ["D211", "D213", "E741", "D105", "E712", "B904"]
ignore = ["B904", "D105", "D211", "D213", "D301", "E712", "E741"]
exclude = ["docs"]

[tool.ruff.per-file-ignores]
Expand Down
2 changes: 1 addition & 1 deletion src/fiat/cfg.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""The config interpreter of FIAT."""

import os
from pathlib import Path
from typing import Any

import tomli
Expand All @@ -12,7 +13,6 @@
check_config_grid,
)
from fiat.util import (
Path,
create_hidden_folder,
flatten_dict,
generic_folder_check,
Expand Down
Loading

0 comments on commit 0dc49d1

Please sign in to comment.