Skip to content

Instantly share code, notes, and snippets.

@sylae
Last active January 28, 2018 01:22
Show Gist options
  • Save sylae/2054dc4f4ea44314502788e828bf1b12 to your computer and use it in GitHub Desktop.
Save sylae/2054dc4f4ea44314502788e828bf1b12 to your computer and use it in GitHub Desktop.
Convert lyx file to markdown and BBcode

rips off all the sample code from the projects composer uses, as well as https://codepen.io/MrHuds0n/full/bgWdqe for the bbcode output.

usage:

composer install
# (put a single .lyx file into the directory)
sh generate.sh

edit generate.sh as needed to fix paths (you'll need to do this, trust me). also check out the top of mdToBBC.php.

this code is all shit patched together with more shit, so that it can handle my very specific and narrow use case.

{
"require": {
"league/html-to-markdown": "^4.4",
"michelf/php-markdown": "^1.7"
}
}
# clean old files
rm *.xhtml _markdown.md _html.html _bbcode.txt
/e/program\ files\ \(x86\)/LyX\ 2.3/bin/lyx --export xhtml *.lyx
cat *.xhtml | php htmlToMD.php > _markdown.md
cat _markdown.md | php MDToHTML.php > _html.html
cat _markdown.md | php mdToBBC.php > _bbcode.txt
<?php
require 'vendor/autoload.php';
use League\HTMLToMarkdown\HtmlConverter;
$converter = new HtmlConverter();
$converter->getConfig()->setOption('strip_tags', true);
$html = file_get_contents("php://stdin");
$markdown = $converter->convert($html);
$filter = "Chapter 1";
/**
* everything up to and including $filter will be trimmed.
* use to eliminate the xml tag and css shit
**/
echo trim(substr($markdown, strpos($markdown, $filter) + strlen($filter)));
<?php
$h1replace = "[size=7][b]$1[/b][/size]";
$h2replace = "[hr][size=6]$1[/size]";
$h3replace = "[size=4]$1[/size]";
$h1regex = "/# (.*?)(?:\\ *)\\n/";
$h2regex = "/## (.*?)(?:\\ *)\\n/";
$h3regex = "/### (.*?)(?:\\ *)\n/";
$h1regexUnderline = "/(.*?)\\n={3,}/";
$h2regexUnderline = "/(.*?)\\n-{3,}/";
$boldItalicRegex = "/(?:\\*\\*\\*|___)(.*?)(?:\\*\\*\\*|___)(?![^\\[]*\\])/";
$boldRegex = "/(?:\\*\\*|__)(.*?)(?:\\*\\*|__)(?![^\\[]*\\])/";
$italicRegex = "/(?:\\*|_)(.*?)(?:\\*|_)(?![^\\[]*\\])/";
$strikethroughRegex = "/~~(.*?)~~/";
$imgRegex = "/!\\[(?:.*?)\\]\\((.*?)\\)/";
$urlRegex = "/\\[(.*?)\\]\\((.*?)\\)/";
$codeRegex = "/```(?:\\ *)\\n(.*?)(?:\\ *)\\n```/";
//Set replace values for headers
$output = file_get_contents("php://stdin");
$output = preg_replace($h3regex, $h3replace, $output);
$output = preg_replace($h2regex, $h2replace, $output);
$output = preg_replace($h2regexUnderline, $h2replace, $output);
$output = preg_replace($h1regex, $h1replace, $output);
$output = preg_replace($h1regexUnderline, $h1replace, $output);
$output = preg_replace($imgRegex, "[img]$1[/img]", $output);
$output = preg_replace($urlRegex, "[url=$2]$1[/url]", $output);
$output = preg_replace($codeRegex, "[code]$1[/code]", $output);
$output = preg_replace($boldItalicRegex, "[b][i]$1[/i][/b]", $output);
$output = preg_replace($boldRegex, "[b]$1[/b]", $output);
$output = preg_replace($italicRegex, "[i]$1[/i]", $output);
$output = preg_replace($strikethroughRegex, "[s]$1[/s]", $output);
echo $output;
<?php
require 'vendor/autoload.php';
use Michelf\MarkdownExtra;
$md = file_get_contents("php://stdin");
$html = MarkdownExtra::defaultTransform($md);
echo $html;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment