0

Suppose I have an original text code made for a phpBB forum, with bold, colors and underline text code, like this (take note that there are some basic LaTeX codes in it):

[size=150][b]Chapitre 4.  La cinématique du mouvement rectiligne (le long d'un seul axe)[/b][/size]

Toutes les définitions et formules suivantes doivent être connues et comprises en détails par les élèves du cours de mise à niveau.

[b][color=#BF0000]Définition :[/color][/b] La [b][u]cinématique[/u][/b] est la partie de la physique qui cherche à [u]décrire le mouvement des corps[/u], sans chercher à expliquer le mouvement.  La cinématique fait appel à la [u]position instantanée[/u] [latex]x(t)[/latex] (position à l'instant [latex]t[/latex]), la [u]vitesse moyenne[/u] [latex]v_{x \, \text{moy}}[/latex] (moyenne entre deux instants), la [u]vitesse instantanée[/u] [latex]v_x(t)[/latex] (vitesse exacte à l'instant [latex]t[/latex]), l'[u]accélération instantanée[/u] [latex]a_x(t)[/latex] et l'[u]accélération moyenne[/u] [latex]a_{x \, \text{moy}}[/latex].

Is it possible to copy and paste the code directly into a LaTeX document and make LaTeX to automatically recognize the various [/]...[] and compile them as normal bold, color and underline commands? If it's too much trouble to tell LaTeX how to do it automatically, my only alternative would be to make various search/replaces, which is pretty easy, but would be very long to do with many phpBB codes...

  • There are various automatic converters that transform BBCode to Markdown (see google.com/search?q=convert+bbcode+to+markdown). From there you can convert to LaTeX using Pandoc. Most of the converters are Javascript-based, so you might need to adapt them a bit in order to process many different files automatically. Also most don't implement the [latex] BBCode tag so you should add some code to change that into $. – Marijn May 8 at 16:37
2

With LuaLaTeX you could with, although it turns out to be a bit complicated (I don't know about phpBB markup language, but I guess you do, so...). I've used pgfkeys, also, as it makes easy to handle with key=value pairs. Here's a proof of concept

%!TEX program = lualatex
\documentclass{article}
\usepackage[margin=2.5cm]{geometry}
\usepackage{luacode}
%Underlines
\usepackage[soul]{lua-ul}
%For HTML color support
\usepackage{xcolor}
%For \text{}
\usepackage{amsmath}
\usepackage{pgfkeys}
%If you need more options, you can add them to pgfkeys
%Sorry, the font size (in points) was too large,
%So I scaled it
\pgfkeys{/phpbb/size/.code=\fontsize{\directlua{tex.sprint(#1/6)}pt}{\directlua{tex.sprint(#1/5)}pt}}
\pgfkeys{/phpbb/color/.code=\color[HTML]{#1}}
\def\phpbb[#1]{\pgfkeys{/phpbb/#1}}
\begin{luacode*}
function phpbb(str)
    str = string.gsub(str, "%[color=#", "{\\phpbb[color=")
    str = string.gsub(str, "%[latex%]", "$")
    str = string.gsub(str, "%[/latex%]", "$")
    str = string.gsub(str, "%[b%]", "\\textbf{")
    str = string.gsub(str, "%[/b%]", "}")
    str = string.gsub(str, "%[u%]", "\\ul{")
    str = string.gsub(str, "%[/u%]", "}")
    str = string.gsub(str, "%[size=", "{\\phpbb[size=")
    str = string.gsub(str, "%[/size%]", "}")
    str = string.gsub(str, "%[/color%]", "}")
    return str
end
\end{luacode*}
\newcommand{\startphpbb}{\directlua{luatexbase.add_to_callback("process_input_buffer", phpbb , "phpbb" )}}
\newcommand{\stopphpbb}{\directlua{luatexbase.remove_from_callback("process_input_buffer", "phpbb" )}}
\begin{document}
\startphpbb
[size=150][b]Chapitre 4.  La cinématique du mouvement rectiligne (le long d'un seul axe)[/b][/size]

Toutes les définitions et formules suivantes doivent être connues et comprises en détails par les élèves du cours de mise à niveau.

[b][color=#BF0000]Définition :[/color][/b] La [b][u]cinématique[/u][/b] est la partie de la physique qui cherche à [u]décrire le mouvement des corps[/u], sans chercher à expliquer le mouvement.  La cinématique fait appel à la [u]position instantanée[/u] [latex]x(t)[/latex] (position à l'instant [latex]t[/latex]), la [u]vitesse moyenne[/u] [latex]v_{x \, \text{moy}}[/latex] (moyenne entre deux instants), la [u]vitesse instantanée[/u] [latex]v_x(t)[/latex] (vitesse exacte à l'instant [latex]t[/latex]), l'[u]accélération instantanée[/u] [latex]a_x(t)[/latex] et l'[u]accélération moyenne[/u] [latex]a_{x \, \text{moy}}[/latex].
\stopphpbb
\end{document}

enter image description here

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.