Skip to content

Allow setting a default language for literal blocks #608

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

Merged
merged 3 commits into from
Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/guides-cli/resources/schema/guides.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
<xsd:attribute name="fail-on-log" type="xsd:string"/>
<xsd:attribute name="show-progress" type="xsd:string"/>
<xsd:attribute name="theme" type="xsd:string"/>
<xsd:attribute name="default-code-language" type="xsd:string"/>
</xsd:complexType>

<xsd:complexType name="extension">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
use phpDocumentor\Guides\RestructuredText\Directives\WarningDirective;
use phpDocumentor\Guides\RestructuredText\Directives\WrapDirective;
use phpDocumentor\Guides\RestructuredText\MarkupLanguageParser;
use phpDocumentor\Guides\RestructuredText\Parser\DocumentParserContextFactory;
use phpDocumentor\Guides\RestructuredText\Parser\InlineParser;
use phpDocumentor\Guides\RestructuredText\Parser\Productions\AnnotationRule;
use phpDocumentor\Guides\RestructuredText\Parser\Productions\BlockQuoteRule;
Expand Down Expand Up @@ -319,6 +320,8 @@
->set(SectionRule::class)
->tag('phpdoc.guides.parser.rst.structural_element', ['priority' => SectionRule::PRIORITY])

->set(DocumentParserContextFactory::class)

->set(MarkupLanguageParser::class)
->args([
'$startingRule' => service(DocumentRule::class),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
use phpDocumentor\Guides\ParserContext;
use phpDocumentor\Guides\RestructuredText\Parser\BlockContext;
use phpDocumentor\Guides\RestructuredText\Parser\DocumentParserContext;
use phpDocumentor\Guides\RestructuredText\Parser\DocumentParserContextFactory;
use phpDocumentor\Guides\RestructuredText\Parser\Productions\Rule;
use phpDocumentor\Guides\RestructuredText\TextRoles\TextRoleFactory;
use RuntimeException;
use Webmozart\Assert\Assert;

Expand All @@ -28,7 +28,7 @@ class MarkupLanguageParser implements ParserInterface
/** @param Rule<DocumentNode> $startingRule */
public function __construct(
private readonly Rule $startingRule,
private readonly TextRoleFactory $textRoleFactory,
private readonly DocumentParserContextFactory $documentParserContextFactory,
) {
}

Expand All @@ -42,7 +42,7 @@ public function getSubParser(): MarkupLanguageParser
{
return new MarkupLanguageParser(
$this->startingRule,
$this->textRoleFactory,
$this->documentParserContextFactory,
);
}

Expand Down Expand Up @@ -75,11 +75,7 @@ public function parse(ParserContext $parserContext, string $contents): DocumentN
{
$this->parserContext = $parserContext;

$this->documentParser = new DocumentParserContext(
$parserContext,
$this->textRoleFactory,
$this,
);
$this->documentParser = $this->documentParserContextFactory->create($this);

$blockContext = new BlockContext($this->documentParser, $contents);
if ($this->startingRule->applies($blockContext)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace phpDocumentor\Guides\RestructuredText\Parser;

use phpDocumentor\Guides\RestructuredText\MarkupLanguageParser;
use phpDocumentor\Guides\RestructuredText\TextRoles\TextRoleFactory;
use phpDocumentor\Guides\Settings\SettingsManager;

class DocumentParserContextFactory
{
public function __construct(
private readonly TextRoleFactory $textRoleFactory,
private readonly SettingsManager $settingsManager,
) {
}

public function create(MarkupLanguageParser $markupLanguageParser): DocumentParserContext
{
$documentParser = new DocumentParserContext(
$markupLanguageParser->getParserContext(),
$this->textRoleFactory,
$markupLanguageParser,
);

$documentParser->setCodeBlockDefaultLanguage($this->settingsManager->getProjectSettings()->getDefaultCodeLanguage());

return $documentParser;
}
}
5 changes: 5 additions & 0 deletions packages/guides/src/DependencyInjection/GuidesExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public function getConfigTreeBuilder(): TreeBuilder
->defaultValue([])
->scalarPrototype()->end()
->end()
->scalarNode('default_code_language')->defaultValue('')->end()
->arrayNode('themes')
->defaultValue([])
->arrayPrototype()
Expand Down Expand Up @@ -159,6 +160,10 @@ public function load(array $configs, ContainerBuilder $container): void
$projectSettings->setFailOnError((bool) $config['show_progress']);
}

if (isset($config['default_code_language'])) {
$projectSettings->setDefaultCodeLanguage((string) $config['default_code_language']);
}

$container->getDefinition(SettingsManager::class)
->addMethodCall('setProjectSettings', [$projectSettings]);

Expand Down
11 changes: 11 additions & 0 deletions packages/guides/src/Settings/ProjectSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class ProjectSettings
private string $logPath = 'php://stder';
private bool $failOnError = false;
private bool $showProgressBar = true;
private string $defaultCodeLanguage = '';

public function getTitle(): string
{
Expand Down Expand Up @@ -135,6 +136,16 @@ public function setOutputFormats(array $outputFormats): void
$this->outputFormats = $outputFormats;
}

public function setDefaultCodeLanguage(string $defaultCodeLanguage): void
{
$this->defaultCodeLanguage = $defaultCodeLanguage;
}

public function getDefaultCodeLanguage(): string
{
return $this->defaultCodeLanguage;
}

public function getInputFile(): string
{
return $this->inputFile;
Expand Down
8 changes: 3 additions & 5 deletions packages/guides/src/Settings/SettingsManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@

class SettingsManager
{
private ProjectSettings $projectSettings;

public function __construct(ProjectSettings|null $projectSettings = null)
{
$this->projectSettings = $projectSettings ?? new ProjectSettings();
public function __construct(
private ProjectSettings $projectSettings = new ProjectSettings(),
) {
}

public function getProjectSettings(): ProjectSettings
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!DOCTYPE html>
<html lang="en">
<head>
<title>Code Block</title>

</head>
<body>
<div class="section" id="code-block">
<h1>Code Block</h1>

<pre><code class="language-php">$some = &#039;PHP&#039;;
</code></pre>
<pre><code class="language-php">$someThing = &#039;else&#039;;</code></pre>
</div>

</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8" ?>
<guides xmlns="https://www.phpdoc.org/guides"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://www.phpdoc.org/guides packages/guides-cli/resources/schema/guides.xsd"
default-code-language="php"
>
</guides>
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Code Block
==========

::

$some = 'PHP';

.. code-block::

$someThing = 'else';