-
-
Notifications
You must be signed in to change notification settings - Fork 59
ADFA-5405: Let templates reference each other #1779
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
davidschachterADFA
wants to merge
9
commits into
stage
Choose a base branch
from
task/ADFA-5405-webserver-multiple-templates
base: stage
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
00493e1
ADFA-5405: Add a Pebble loader that reads the Templates table
davidschachterADFA e905093
ADFA-5405: Load templates by name, so they can reference each other
davidschachterADFA e362822
ADFA-5405: Render the bookshelf by name, dropping its id cache
davidschachterADFA ccc34a1
ADFA-5405: Name the unresolvable template in the /pr/bs error
davidschachterADFA 6e6d865
ADFA-5405: Handle a reference cycle, and clean up the render diagnostics
davidschachterADFA 59d7328
ADFA-5405: Correct the stale swap comment on serveRequest
davidschachterADFA cdaf2da
ADFA-5405: Keep a parse error's file and line, and bound a runaway re…
davidschachterADFA 4e2e619
ADFA-5405: Address the review, and close the leak one layer deeper
davidschachterADFA 30cef44
Merge branch 'stage' into task/ADFA-5405-webserver-multiple-templates
davidschachterADFA File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
common/src/main/java/com/itsaky/androidide/documentation/DatabaseTemplateLoader.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| /* | ||
| * This file is part of Code on the Go. | ||
| * | ||
| * AndroidIDE is free software: you can redistribute it and/or modify | ||
| * it under the terms of the GNU General Public License as published by | ||
| * the Free Software Foundation, either version 3 of the License, or | ||
| * (at your option) any later version. | ||
| * | ||
| * AndroidIDE is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU General Public License | ||
| * along with AndroidIDE. If not, see <https://www.gnu.org/licenses/>. | ||
| */ | ||
|
|
||
| package com.itsaky.androidide.documentation | ||
|
|
||
| import android.database.sqlite.SQLiteDatabase | ||
| import io.pebbletemplates.pebble.error.LoaderException | ||
| import io.pebbletemplates.pebble.loader.Loader | ||
| import java.io.Reader | ||
| import java.io.StringReader | ||
|
|
||
| /** | ||
| * Resolves Pebble template names against the `Templates` table, so a template can pull in another | ||
| * one with `extends`, `include`, `import` or `embed` (ADFA-5405). | ||
| * | ||
| * This replaces Pebble's `StringLoader`, which treats the name it is handed *as* the template body. | ||
| * That works for one self-contained template and silently breaks every cross-reference: | ||
| * `{% include "nav.peb" %}` asks the loader for "nav.peb", `StringLoader` hands back those eight | ||
| * characters as a template, and the page renders the literal text instead of the partial -- no | ||
| * exception, no log line. | ||
| * | ||
| * Names are `Templates.name` values, matched exactly: the table is a flat namespace with no | ||
| * directories, so there is no prefix, suffix or relative path to apply. | ||
| * | ||
| * @param database Supplies the database to read, or null when none is open. Called on every | ||
| * resolution rather than captured, because the source swaps the handle when a newer database | ||
| * appears; callers resolve under the read lock that a swap excludes, so the handle cannot change | ||
| * mid-render. | ||
| */ | ||
| internal class DatabaseTemplateLoader( | ||
| private val database: () -> SQLiteDatabase?, | ||
| ) : Loader<String> { | ||
| override fun getReader(name: String): Reader { | ||
| val database = database() ?: throw LoaderException(null, "No documentation database is open, for template '$name'") | ||
|
|
||
| return database.rawQuery(TEMPLATE_QUERY, arrayOf(name)).use { cursor -> | ||
| if (!cursor.moveToFirst()) { | ||
| throw LoaderException(null, "Template '$name' not found in the database") | ||
| } | ||
| StringReader(cursor.getBlob(0).toString(Charsets.UTF_8)) | ||
| } | ||
| } | ||
|
|
||
| override fun resourceExists(name: String): Boolean { | ||
| val database = database() ?: return false | ||
|
|
||
| // Not TEMPLATE_QUERY: that copies the whole template blob into a CursorWindow to answer a | ||
| // boolean. Pebble reaches this only through the delegating and servlet loaders, neither of | ||
| // which is wired here, so the cost would be invisible -- which is the reason to get it right. | ||
| return database.rawQuery(EXISTS_QUERY, arrayOf(name)).use { it.moveToFirst() } | ||
| } | ||
|
|
||
| override fun createCacheKey(name: String): String = name | ||
|
|
||
| /** The table is a flat namespace, so a reference resolves to itself -- as with Pebble's own `MemoryLoader`. */ | ||
| override fun resolveRelativePath( | ||
| relativePath: String, | ||
| anchorPath: String, | ||
| ): String = relativePath | ||
|
|
||
| /** Template bodies are stored as UTF-8 blobs, so the engine's charset setting does not apply. */ | ||
| override fun setCharset(charset: String) = Unit | ||
|
|
||
| /** Names are exact `Templates.name` values; decorating them would stop them matching. */ | ||
| override fun setPrefix(prefix: String) = Unit | ||
|
|
||
| override fun setSuffix(suffix: String) = Unit | ||
|
|
||
| private companion object { | ||
| private const val TEMPLATE_QUERY = "SELECT content FROM Templates WHERE name = ?" | ||
| private const val EXISTS_QUERY = "SELECT 1 FROM Templates WHERE name = ? LIMIT 1" | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.