Skip to content

Commit

Permalink
quick includes from local folders
Browse files Browse the repository at this point in the history
  • Loading branch information
alaindresse committed Sep 24, 2024
1 parent e017e77 commit d6b49aa
Show file tree
Hide file tree
Showing 11 changed files with 88 additions and 5 deletions.
3 changes: 2 additions & 1 deletion i18n/de/package.i18n.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,5 +78,6 @@
"asciidoc.use_kroki.deprecationMessage": "Diese Einstellung wurde durch `#asciidoc.extensions.enableKroki#` ersetzt und hat keine Auswirkungen mehr.",

"asciidoc.antora.title": "Antora",
"asciidoc.antora.enableAntoraSupport.desc": "Aktiviere [Antora](https://antora.org/) Unterstützung."
"asciidoc.antora.enableAntoraSupport.desc": "Aktiviere [Antora](https://antora.org/) Unterstützung.",
"asciidoc.antora.enableLocalIncludes.desc": "Aktiviert lokale Includes aus familienordnern, wenn Antora-Unterstützung deaktiviert ist."
}
1 change: 1 addition & 0 deletions i18n/fra/package.i18n.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@

"asciidoc.antora.title": "Antora",
"asciidoc.antora.enableAntoraSupport.desc": "Active le support [Antora](https://antora.org/).",
"asciidoc.antora.enableLocalIncludes.desc": "Active les inclusions locales à partir des dossiers de famille si le support d'Antora est désactivé.",

"asciidoc.pdf.title": "PDF",
"asciidoc.pdf.engine.desc": "Contrôle le moteur PDF à utiliser pour l'export PDF.",
Expand Down
1 change: 1 addition & 0 deletions i18n/jpn/package.i18n.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@

"asciidoc.antora.title": "Antora",
"asciidoc.antora.enableAntoraSupport.desc": "[Antora](https://antora.org/)サポートを有効にします。",
"asciidoc.antora.enableLocalIncludes.desc": "Antoraサポートが無効になっている場合、ファミリーフォルダーからのローカルインクルードを有効にします。",

"asciidoc.pdf.title": "PDF",
"asciidoc.pdf.engine.desc": "PDFエキスポートで使用するPDFエンジンを選択します。",
Expand Down
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,11 @@
"type": "boolean",
"default": false,
"markdownDescription": "%asciidoc.antora.enableAntoraSupport.desc%"
},
"asciidoc.antora.enableLocalIncludes": {
"type": "boolean",
"default": true,
"markdownDescription": "%asciidoc.antora.enableLocalIncludes.desc%"
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion package.nls.de.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,6 @@
"asciidoc.use_kroki.desc": "**Veraltet:** Aktiviert die Kroki-Integration zur Generierung von Diagrammen.",
"asciidoc.use_kroki.deprecationMessage": "Diese Einstellung wurde durch `#asciidoc.extensions.enableKroki#` ersetzt und hat keine Auswirkungen mehr.",
"asciidoc.antora.title": "Antora",
"asciidoc.antora.enableAntoraSupport.desc": "Aktiviere [Antora](https://antora.org/) Unterstützung."
"asciidoc.antora.enableAntoraSupport.desc": "Aktiviere [Antora](https://antora.org/) Unterstützung.",
"asciidoc.antora.enableLocalIncludes.desc": "Aktiviert lokale Includes aus familienordnern, wenn Antora-Unterstützung deaktiviert ist."
}
3 changes: 2 additions & 1 deletion package.nls.fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,6 @@
"asciidoc.use_kroki.desc": "**Obsolète:** Active l'extension Kroki afin de générer des diagrammes.",
"asciidoc.use_kroki.deprecationMessage": "Ce paramètre a été remplacé par `#asciidoc.extensions.enableKroki#` et n'a plus aucun effet.",
"asciidoc.antora.title": "Antora",
"asciidoc.antora.enableAntoraSupport.desc": "Active le support [Antora](https://antora.org/)."
"asciidoc.antora.enableAntoraSupport.desc": "Active le support [Antora](https://antora.org/).",
"asciidoc.antora.enableLocalIncludes.desc": "Active les inclusions locales à partir des dossiers de famille si le support d'Antora est désactivé."
}
3 changes: 2 additions & 1 deletion package.nls.ja.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,6 @@
"asciidoc.use_kroki.desc": "**非推奨:** ダイアグラム生成Krokiを使用します。",
"asciidoc.use_kroki.deprecationMessage": "本設定は、`#asciidoc.extensions.enableKroki#`に置き換えられ、動作しません。",
"asciidoc.antora.title": "Antora",
"asciidoc.antora.enableAntoraSupport.desc": "[Antora](https://antora.org/)サポートを有効にします。"
"asciidoc.antora.enableAntoraSupport.desc": "[Antora](https://antora.org/)サポートを有効にします。",
"asciidoc.antora.enableLocalIncludes.desc": "Antoraサポートが無効になっている場合、ファミリーフォルダーからのローカルインクルードを有効にします。"
}
3 changes: 2 additions & 1 deletion package.nls.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,5 +78,6 @@
"asciidoc.use_kroki.deprecationMessage": "This setting has been replaced by `#asciidoc.extensions.enableKroki#` and no longer has any effect.",

"asciidoc.antora.title": "Antora",
"asciidoc.antora.enableAntoraSupport.desc": "Enable [Antora](https://antora.org/) support."
"asciidoc.antora.enableAntoraSupport.desc": "Enable [Antora](https://antora.org/) support.",
"asciidoc.antora.enableLocalIncludes.desc": "Enable local include from family folders if antora support is disabled."
}
4 changes: 4 additions & 0 deletions src/asciidocEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { AsciidoctorProcessor } from './asciidoctorProcessor'
import { AsciidoctorAttributesConfig } from './features/asciidoctorAttributesConfig'
import { IncludeProcessor } from './features/antora/includeProcessor'
import { resolveIncludeFile } from './features/antora/resolveIncludeFile'
import { registerLocalAntoraProcessors } from './features/antora/resolveLocalIncludeFile'

const highlightjsAdapter = require('./highlightjs-adapter')

Expand Down Expand Up @@ -140,6 +141,9 @@ export class AsciidocEngine {
await this.asciidoctorExtensionsProvider.activate(registry)
const textDocumentUri = textDocument.uri
await this.asciidoctorConfigProvider.activate(registry, textDocumentUri)

registerLocalAntoraProcessors(registry)

if (antoraDocumentContext !== undefined) {
const antoraConfig = await getAntoraConfig(textDocumentUri)
registry.includeProcessor(IncludeProcessor.$new((_, target, cursor) => resolveIncludeFile(
Expand Down
5 changes: 5 additions & 0 deletions src/asciidocLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { AsciidoctorIncludeItemsProvider, IncludeItems } from './features/asciid
import { getAntoraDocumentContext, getAntoraConfig } from './features/antora/antoraSupport'
import { IncludeProcessor } from './features/antora/includeProcessor'
import { resolveIncludeFile } from './features/antora/resolveIncludeFile'
import { registerLocalAntoraProcessors } from './features/antora/resolveLocalIncludeFile'

export class AsciidocLoader {
protected readonly processor: Asciidoctor
Expand Down Expand Up @@ -56,6 +57,10 @@ export class AsciidocLoader {
await this.asciidoctorExtensionsProvider.activate(registry)
const textDocumentUri = textDocument.uri
await this.asciidoctorConfigProvider.activate(registry, textDocumentUri)

registerLocalAntoraProcessors(registry)


const antoraDocumentContext = await getAntoraDocumentContext(textDocument.uri, this.context.workspaceState)
if (antoraDocumentContext !== undefined) {
const antoraConfig = await getAntoraConfig(textDocumentUri)
Expand Down
62 changes: 62 additions & 0 deletions src/features/antora/resolveLocalIncludeFile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import vscode from 'vscode'
import { Asciidoctor } from '@asciidoctor/core'
import { readFileSync } from 'fs'

const PERMITTED_FAMILIES = ['attachment', 'example', 'image', 'page', 'partial']

function extractPartialModuleAndFilename (resourceId: string): { module?: string, family?: string, filename?: string } {
const regexString = `^(?:(.*):)?(${PERMITTED_FAMILIES.join('|')})\\$(.*)$`
const regex = new RegExp(regexString)
const matches = regex.exec(resourceId)
if (!matches) {
return {}
}

const [, module, family, filename] = matches
return { module, family, filename }
}

function isLocalAntoraIncludeEnabled () {
const workspaceConfiguration = vscode.workspace.getConfiguration('asciidoc', null)

// if Antora support is enabled, don't enable local includes
const enableAntoraSupport = workspaceConfiguration.get('antora.enableAntoraSupport') || false
if (enableAntoraSupport) {
return false
}

return workspaceConfiguration.get('antora.enableLocalIncludes')
}

const localFamilyInclude = function (this: Asciidoctor.Extensions.IncludeProcessorDsl): void {
const self = this
self.handles(function (target) {
if (!isLocalAntoraIncludeEnabled()) {
return false
}
const { filename } = extractPartialModuleAndFilename(target)
return filename !== undefined
})
self.process(function (doc, reader, target, attrs) {
const { module, family, filename } = extractPartialModuleAndFilename(target)

const currentDocumentPath = doc.getBaseDir()

const [antoraPath, currentRelativePath] = currentDocumentPath.split('/modules/')
const currentModule = currentRelativePath.split('/')[0]

const adjustedPath = `${antoraPath}/modules/${module || currentModule}/${family}s/${filename}`

try {
const content = readFileSync(adjustedPath, 'utf8')
reader.pushInclude(content, target, target, 1, attrs)
} catch (e) {
reader.getLogger().error(e)
reader.pushInclude(`Unresolved include: ${target} (tried ${adjustedPath})`, target, target, 1, attrs)
}
})
}

export function registerLocalAntoraProcessors (registry: Asciidoctor.Extensions.Registry) {
registry.includeProcessor(localFamilyInclude)
}

0 comments on commit d6b49aa

Please sign in to comment.