Pygments_Portugol
Pygments é um excelente pacote em Python destinado à coloração de sintaxe (syntax highlighting), sendo possível utilizá-lo, inclusive, em documentos LaTeX. Este pacote fornece um lexer para a linguagem Portugol Studio.
Instalação
Após a instalação do Python 3, você provavelmente poderá executar o comando a seguir a fim de instalar o Pygments:
$ pip install pygments
Note que
$indica o prompt do shell, você não deve digitar isso.
Algumas variações desse comando podem aparecer a depender da plataforma, por exemplo:
$ pip3 install pygments
$ python3 -m pip install pygments
$ python -m pip install pygments
Após instalar com sucesso o Pygments, você pode instalar o lexer para Portugol Studio:
$ pip install pygments-portugol
Utilização
Para utilizar o lexer, o procedimento é equivalente a qualquer outra linguagem suportada pelo Pygments. Utilize portugol ou portugolstudio como nome da linguagem. Por exemplo, o código em Python a seguir ilustra como ler um arquivo code.por e salvar em HTML no arquivo formatted.html.
from pygments import highlight
from pygments.lexers import get_lexer_by_name
from pygments.formatters import HtmlFormatter
with open("./code.por", "r") as fl:
code = fl.read()
lexer = get_lexer_by_name("portugol", stripall=True)
formatter = HtmlFormatter(linenos=True, noclasses=True)
result = highlight(code, lexer, formatter)
with open("./formatted.html", "w") as fl:
fl.write(result)
Clique aqui para ver o resultado.
Utilização com LaTeX
Para tipografar um código em LaTeX com o Pygments, pode-se utilizar o pacote minted. O código a seguir é um exemplo. A fonte DejaVuSansMono foi utilizada apenas por questões estéticas. As duas linhas referentes à fonte podem ser removidas ou substituídas. Caso queira usar pdfLaTeX, remova-as.
% !TeX program = xelatex
\documentclass{article}
\usepackage{fontspec} % remover caso esteja usando pdfLaTeX
\usepackage{DejaVuSansMono} % remover caso esteja usando pdfLaTeX
\setmonofont[Scale=MatchLowercase]{DejaVuSansMono} % remover caso esteja usando pdfLaTeX
\usepackage{minted} % importa o pacote para que possamos usar
\begin{document}
O código a seguir ilustra um fatorial recursivo no Portugol Studio.
\begin{minted}[autogobble, linenos]{portugol}
programa
{
funcao inicio()
{
inteiro numero
escreva("Digite um número: ")
leia(numero)
limpa()
escreva("O fatorial de ", numero, " é: ", fatorial(numero), "\n")
}
// Função recursiva que calcula o fatorial do número passado
funcao inteiro fatorial(inteiro numero)
{
se (numero == 1 ou numero == 0)
{
retorne 1
}
retorne numero * fatorial(numero - 1)
}
}
\end{minted}
\end{document}
Veja este tutorial se quiser ver outras possibilidades, como escolher cores diferentes ou importar de um arquivo externo.
Utilização em LaTeX no Overleaf
Caso você tenha o costume de usar Overleaf, pode ter lido a seção anterior e pensado que é impossível instalar um pacote Python no Overleaf. Sim, é verdade. Felizmente, é possível utilizar um lexer sem instalação obrigatória.
-
Copie o arquivo do lexer (este) e salve como, por exemplo,
PortugolStudioLexer.py. -
Coloque no mesmo diretório do seu arquivo
.tex; -
Substitua o nome da linguagem por
PortugolStudioLexer.py:PortugolStudioLexer -x. -
O código deve ser assim (veja o exemplo completo na seção anterior):
% ...
\begin{minted}[autogobble, linenos]{PortugolStudioLexer.py:PortugolStudioLexer -x}
// seu código
\end{minted}
% ...
Isto pode ser recomendado para projetos maiores, para reduzir a quantidade de dependências que outros colaboradores precisarão instalar (o mesmo vale para fontes personalizadas). Para projetos menores, entretanto, é mais prático instalar e apenas utilizar o nome da linguagem.
Contribuição
Caso note algo de errado, fique à vontade para abrir uma issue ou, ainda melhor, enviar o seu Pull Request.
Autor
Desenvolvido por Héliton Martins.
Metadata
Release files for pygments-portugol 0.2.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| pygments_portugol-0.2.1.tar.gz | 17.3 kB | Without provenance | Details for pygments_portugol-0.2.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| pygments_portugol-0.2.1-py3-none-any.whl | Python 3 | none | any | Details for pygments_portugol-0.2.1-py3-none-any.whl |
Total release size: 35.0 kB
Release files / pygments_portugol-0.2.1.tar.gz
| Download URL | pygments_portugol-0.2.1.tar.gz |
|---|---|
| Size | 17.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
4cb3c25530d40cec5d9e7f0612054974c7cdf69447073d26a0862499e4f65cab
|
|
BLAKE2b-256 checksum How to use checksums |
8ba5e7e71ae6a4434437180102b0aebfbc35fcb0d52ef62c841d710b08c7db87
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.7.1 importlib_metadata/4.10.1 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.2
|
Release files / pygments_portugol-0.2.1-py3-none-any.whl
| Download URL | pygments_portugol-0.2.1-py3-none-any.whl |
|---|---|
| Size | 17.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
51b9230d8ab45ac6f2f6b6c6d8d5426619157028469c295ae8e3eb427486b94f
|
|
BLAKE2b-256 checksum How to use checksums |
90fc32d73e4d4c93b4e9f127a5d03a94af439bf8deb994b831ee8be9116e0302
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.7.1 importlib_metadata/4.10.1 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.2
|
Security for pygments-portugol 0.2.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.