@@ -101,22 +101,31 @@ '(pagenode title published noun_plural noun_singular)) + +(define table_keywordindex-fields + '(entry + subentry + pagenode + anchor)) (define table_articles (make-table-schema "articles" table_articles-fields)) (define table_notes (make-table-schema "notes" table_notes-fields #:primary-key-cols '(pagenode note_id))) (define table_series (make-table-schema "series" table_series-fields)) +(define table_keywordindex (make-table-schema "keywordindex" + table_keywordindex-fields + #:primary-key-cols '(pagenode anchor))) ;; ~~~ Provided functions: Initializing; Saving posts and notes ;; Initialize the database connection, creating the database if it doesn’t ;; exist, and executing the table schema queries ;; (define (spell-of-summoning!) - (init-db! DBFILE table_articles table_notes table_series)) + (init-db! DBFILE table_articles table_notes table_series table_keywordindex)) ;; Save an article and its notes (if any) to the database, and return the ;; rendered HTML of the complete article. ;; (define (crystalize-article! pagenode doc) @@ -137,10 +146,12 @@ [series-node (series-pagenode)] [header (html$-article-open pagenode title-specified? title-tx pubdate)] [footertext (make-article-footertext pagenode series-node disposition disp-note-id (length note-txprs))] [footer (html$-article-close footertext)] [notes-section-html (crystalize-notes! pagenode title-plain note-txprs)]) + + (crystalize-index-entries! pagenode body-txpr) ;; Values must come in the order defined in table_article_fields (define article-record (list (symbol->string pagenode) title-plain @@ -336,10 +347,32 @@ (html$-note-in-article note-id note-date content-html author author-url)) (define (article-plain-title pagenode) (query-value (sqltools:dbc) "SELECT `title_plain` FROM `articles` WHERE `pagenode` = ?1" (symbol->string pagenode))) +;; ~~~ Keyword Index Entries ~~~ + +;; (private) Save any index entries in doc to the cache +(define (crystalize-index-entries! pagenode doc) + (define (index-entry? tx) + (and (txexpr? tx) + (string=? "index-link" (attr-ref tx 'class "")) ; see definition of html-index + (attr-ref tx 'data-index-entry #f))) + (define-values (_ entries) (splitf-txexpr doc index-entry?)) + + ; Naive idempotence: delete and re-insert all index entries every time doc is rendered. + (query! "DELETE FROM `keywordindex` WHERE `pagenode` = ?1" (symbol->string pagenode)) + + (unless (null? entries) + (define entry-rows + (for/list ([entry-tx (in-list entries)]) + (list (attr-ref entry-tx 'data-index-entry) + "" ; subentries not yet implemented + (symbol->string pagenode) + (attr-ref entry-tx 'id)))) + (query! (make-insert-rows-query "keywordindex" table_keywordindex-fields entry-rows)))) + ;; ~~~ Series ~~~ ;; Preloads the SQLite cache with info about each series. ;; I may not actually need this but I’m leaving it for now. (define (preheat-series!)