Skip to content

Commit

Permalink
feat: prettify html output
Browse files Browse the repository at this point in the history
  • Loading branch information
PedroTroller committed Nov 4, 2020
1 parent 5b54716 commit b436a6f
Show file tree
Hide file tree
Showing 11 changed files with 1,250 additions and 430 deletions.
10 changes: 7 additions & 3 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
vendor
Dockerfile
.*
*

!/Gemfile*
!/composer*
!/features/
!/index.php
!/package*
3 changes: 2 additions & 1 deletion .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,6 @@ updates:
schedule:
interval: "daily"
commit-message:
prefix: build
prefix: fix
prefix-development: build
target-branch: beta
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
vendor
/node_modules/
/vendor/
30 changes: 18 additions & 12 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,43 +4,49 @@ FROM composer:2.0.4 as composer

FROM php:7.4.12-apache AS prod

RUN apt-get update \
RUN (curl -sL https://deb.nodesource.com/setup_14.x | bash) \
&& apt-get update \
&& apt-get install -y --no-install-recommends \
git \
libghc-gnuidn-dev \
locales \
nodejs \
perl \
python \
python-pip \
ruby-dev \
unzip \
locales \
zlib1g-dev \
&& pip install docutils==0.14 \
&& gem install bundler \
&& rm -rf /var/lib/apt/lists/*

RUN bundle config set no-cache 'true'

COPY --from=composer /usr/bin/composer /usr/bin/composer

RUN echo en_US.UTF-8 UTF-8 > /etc/locale.gen && \
locale-gen && \
locale -a

ENV LANG=en_US.UTF-8

RUN pip install docutils==0.14

RUN gem install bundler

COPY --from=composer /usr/bin/composer /usr/bin/composer
ENV PATH /var/www/html/node_modules/.bin:$PATH

WORKDIR /var/www/html

COPY Gemfile* /var/www/html/
COPY composer.* /var/www/html/
RUN composer install --no-scripts --no-dev
COPY package* /var/www/html/

COPY Gemfile* /var/www/html/
RUN bundler install
RUN bundler install --jobs $(($(nproc) * 2)) \
&& composer install --no-scripts --no-dev \
&& npm install

COPY . /var/www/html

########################################

FROM prod AS dev

RUN composer install --no-scripts --no-dev
RUN composer install --no-scripts \
&& npm install --dev
2 changes: 1 addition & 1 deletion behat.yaml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ default:
default:
contexts:
- FeatureContext:
host: "http://localhost/"
host: "http://prod/"
2 changes: 2 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@ services:
target: dev
volumes:
- '.:/var/www/html/'
depends_on:
- prod
16 changes: 4 additions & 12 deletions features/FeatureContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,24 +65,16 @@ public function iShouldGetTheFollowingHtml(PyStringNode $html): void

try {
Assert::eq(
$this->cleanupHtml((string) $this->response->getBody()),
$this->cleanupHtml((string) $html)
(string) $this->response->getBody(),
(string) $html,
);
} catch (InvalidArgumentException $exception) {
echo (new Differ())->diff(
$this->cleanupHtml((string) $html),
$this->cleanupHtml((string) $this->response->getBody())
(string) $html,
(string) $this->response->getBody(),
);

throw $exception;
}
}

private function cleanupHtml(string $html): string
{
$html = str_replace("\t", '', $html);
$html = trim($html, " \n");

return $html;
}
}
32 changes: 19 additions & 13 deletions features/tests.feature
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Feature: Parse markup file and transform them
}
}
</code></pre>
"""

Scenario Outline: Parse textile format
Expand All @@ -43,12 +44,16 @@ Feature: Parse markup file and transform them
"""
<h2>Textile</h2>
<ul>
<li>is a <em>shorthand syntax</em> used to generate valid <span class="caps">HTML</span>,</li>
<li>is <strong>easy</strong> to read and <strong>easy</strong> to write,</li>
<li>can generate complex pages,</li>
<li>including headers, quotes, lists, tables, and figures.</li>
<li>is a <em>shorthand syntax</em> used to generate valid <span class="caps">HTML</span>,</li>
<li>is <strong>easy</strong> to read and <strong>easy</strong> to write,</li>
<li>can generate complex pages,</li>
<li>including headers, quotes, lists, tables, and figures.</li>
</ul>
<p style="font-size:0.8em;"><strong>TxStyle</strong> is a documentation project of Textile for <a href="http://textpattern.com">Textpattern <span class="caps">CMS</span></a>.</p>
<p style="font-size: 0.8em">
<strong>TxStyle</strong> is a documentation project of Textile for <a href="http://textpattern.com">Textpattern <span class="caps">CMS</span></a
>.
</p>
"""

Examples:
Expand All @@ -74,9 +79,8 @@ Feature: Parse markup file and transform them
"""
Then I should get the following html
"""
<p># Foo. # # @example # # y # g # # @param [String] param_name The xx and xx. # # @see <a href="http://url.com">url.com</a> # # @return [true] if so</p>
<p># Foo. # # @example # # y # g # # @param [String] param_name The xx and
xx. # # @see <a href="http://url.com">url.com</a> # # @return [true] if so</p>
"""

Scenario Outline: Parse org mode format
Expand All @@ -93,6 +97,7 @@ Feature: Parse markup file and transform them
<h1>Test Agenda</h1>
<p>&lt;2017-03-10 Fri&gt;</p>
<h1>test agenda</h1>
"""

Examples:
Expand Down Expand Up @@ -120,16 +125,17 @@ Feature: Parse markup file and transform them
"""
<p>To localize a postal address, a minimum set of properties are required :</p>
<blockquote>
<ul class="simple">
<li><code>streetAddress</code></li>
<li><code>addressLocality</code></li>
<li><code>postalCode</code></li>
<li><code>addressCountry</code></li>
</ul>
<ul class="simple">
<li><code>streetAddress</code></li>
<li><code>addressLocality</code></li>
<li><code>postalCode</code></li>
<li><code>addressCountry</code></li>
</ul>
</blockquote>
<p>If at least one of these properties is not available, then the <code>address</code> attribute is mandatory.</p>
<p>Without this kind of informations, then a <code>PostalAddress</code> is useless.</p>
<p>The <code>name</code> property of a <code>PostalAddress</code> is optionnal.</p>
"""

Examples:
Expand Down
68 changes: 58 additions & 10 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,21 +44,69 @@
return $formats[$type];
};

$app = new \Slim\App();
$markupToHtml = function (string $markup, string $extension): string {
$temporaryFile = sprintf('%s/%s.%s', sys_get_temp_dir(), uniqid(), $extension);
$process = new Process(
[
'github-markup',
$temporaryFile,
]
);

file_put_contents($temporaryFile, $markup);
$process->run();
unlink($temporaryFile);

if ($process->isSuccessful()) {
return $process->getOutput();
}

$app->post('/', function (Request $request, Response $response, array $args) use ($getExtensionFromRequest): void {
$temporaryFile = sprintf('%s/%s.%s', sys_get_temp_dir(), uniqid(), $getExtensionFromRequest($request));
$input = (string) $request->getBody();
throw new Exception($process->getErrorOutput());
};

file_put_contents($temporaryFile, $input);
$prettier = function (string $html): string {
$temporaryFile = sprintf('%s/%s.%s', sys_get_temp_dir(), uniqid(), 'html');
$process = new Process(
[
'prettier',
'--write',
'--ignore-unknown',
'--parser',
'html',
'--tab-width',
2,
'--print-width',
1000,
$temporaryFile,
]
);

file_put_contents($temporaryFile, $html);
$process->run();
$html = (string) file_get_contents($temporaryFile);
unlink($temporaryFile);

if ($process->isSuccessful()) {
return $html;
}

$process = new Process(['github-markup', $temporaryFile]);
throw new Exception($process->getErrorOutput());
};

$process->run(function (string $type, string $message) use ($response): void {
$response->getBody()->write($message);
});
$app = new \Slim\App();

$response->withStatus($process->isSuccessful() ? 200 : 500);
$app->post('/', function (Request $request, Response $response, array $args) use ($getExtensionFromRequest, $markupToHtml, $prettier): void {
$extension = $getExtensionFromRequest($request);
$markup = (string) $request->getBody();

try {
$html = $markupToHtml($markup, $extension);
$html = $prettier($html);
$response->getBody()->write($html);
$response->withStatus(200);
} catch (Exception $exception) {
$response->withStatus(500);
}
});

$app->get('/_ping', function (Request $request, Response $response, array $args): void {
Expand Down
Loading

0 comments on commit b436a6f

Please sign in to comment.