Skip to content

Instantly share code, notes, and snippets.

@be1
Last active May 16, 2021
Embed
What would you like to do?
Quick (go version) program to generate a LaTeX cdcover from an xml Brasero project.
package main
import (
"os"
"time"
"strings"
"fmt"
"io/ioutil"
"encoding/xml"
"net/url"
)
type Braseroaudio struct {
XMLName xml.Name `xml:"audio"`
Uri string `xml:"uri"`
Start uint64 `xml:"start"`
End uint64 `xml:"end"`
Title string `xml:"title"`
Artist string `xml:"artist"`
}
type Braserotrack struct {
XMLName xml.Name `xml:"track"`
Audio []Braseroaudio `xml:"audio"`
}
type Braseroproject struct {
XMLName xml.Name `xml:"braseroproject"`
Version string `xml:"version"`
Label string `xml:"label"`
Track Braserotrack `xml:"track"`
}
func escape(s string) string {
escape := map[string]string {
"_": "\\_",
"#": "\\#",
"$": "\\$",
"%": "\\%",
"^": "\\^",
"&": "\\&",
"{": "\\{",
"}": "\\}",
"~": "\\~",
}
var escaped = s
escaped = strings.Replace(escaped, "\\", "\\textbackslash", -1)
for k, v := range escape {
escaped = strings.Replace(escaped, k, v, -1)
}
return escaped
}
func main() {
var args = os.Args[1:]
if len(args) != 1 {
fmt.Println("missing brasero project file")
os.Exit(1)
}
content, err := ioutil.ReadFile(args[0])
if (err != nil) {
fmt.Println("error reading brasero project file")
os.Exit(1)
}
var x Braseroproject;
if err := xml.Unmarshal(content, &x); err != nil {
fmt.Println("xml error parsing brasero project")
os.Exit(1)
}
var tex_begin = `\documentclass{cd-cover}[a4paper,landscape]
\usepackage[french]{babel}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{sans}
`
var com = fmt.Sprintf("\\newcommand{\\TITLE}{%s}\n", x.Label)
tex_begin += com
com = fmt.Sprintf("\\newcommand{\\SUBTITLE}{%s}\n", "Compilation")
tex_begin += com
var date = time.Now()
com = fmt.Sprintf("\\newcommand{\\DATE}{%02d-%02d-%d}\n", date.Day(), date.Month(), date.Year());
tex_begin += com
com = `
\begin{document}
\parindent=0pt\parskip=0pt
\begin{bookletsheets}
\begin{center}
\begin{framebox}
{\LARGE \textbf{\TITLE}}
\end{framebox}
\end{center}
\vfill
{\large{\textbf\SUBTITLE}}\hfill{\large\DATE}
\end{bookletsheets}
\begin{backsheet}{\hfil\TITLE\hfil\SUBTITLE\hfil\DATE}
\begin{center}
\textbf{\TITLE}
\end{center}
\begin{enumerate}
`
tex_begin += com
fmt.Println(tex_begin)
for i := 0; i < len(x.Track.Audio); i++ {
audio := x.Track.Audio[i]
title, _ := url.QueryUnescape(audio.Title)
artist, _ := url.QueryUnescape(audio.Artist)
duration := (audio.End - audio.Start) / 1000000
sec := duration / 1000
min := sec / 60
sec = sec % 60
fmt.Printf("\\item %s (%s) \\dotfill %d'%02d\n", escape(title), escape(artist), min, sec)
}
fmt.Println("\\end{enumerate}")
fmt.Println()
fmt.Println("\\end{backsheet}")
fmt.Println("\\end{document}")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment