Skip to content

Commit

Permalink
Outillage : préfixage des listings avec un $ non-sélectionnable
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Parisot committed Jan 23, 2018
1 parent 8909946 commit 3de2f1f
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
2 changes: 2 additions & 0 deletions bin/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const {join} = require('path');
const processor = require('asciidoctor.js')();
const runnerExtension = require('asciidoctor-extension-interactive-runner');
const bash$Extension = require('../src/asciidoctor-extension-bash-dollar');
const BUILD_DIR = 'dist';

var DEFAULT_ATTRIBUTES = [
Expand All @@ -20,6 +21,7 @@ var DEFAULT_ATTRIBUTES = [
const FILES = process.argv.slice(2);

processor.Extensions.register(runnerExtension);
processor.Extensions.register(bash$Extension);

FILES.forEach(SOURCE_FILE => {
const destinationFile = SOURCE_FILE.replace('.adoc', '.html');
Expand Down
44 changes: 44 additions & 0 deletions src/asciidoctor-extension-bash-dollar.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
'use strict';

const isBash = (b) => b.getAttribute('language', 'bash');
const index = (blocks, block) => blocks.findIndex(el => el === block);

module.exports = function bash$Extension () {
this.treeProcessor(function(){
this.process(doc => {
doc.findBy({ context: 'listing' }, isBash).forEach(block => {
const {parent} = block;

let isModified = false;

const lines = block.lines.map(line => {
if (line[0] === '$' && line[1] === ' ') {
isModified = true;
return '<span class=dollar></span>' + line.slice(2);
}

return line;
});

if (isModified) {
const blockIndex = index(parent.getBlocks(), block);

block.lines = lines;
block.$remove_sub('specialcharacters');

parent.blocks[blockIndex] = block;
}
});
});
});

this.docinfoProcessor(function(){
this.process(doc => {
return `<style type="text/css">
.listingblock .dollar::before{
content: "$ ";
opacity: .5; }
</style>`;
});
});
};

0 comments on commit 3de2f1f

Please sign in to comment.