feat(tester): integrate pandoc
This commit is contained in:
parent
ffacc9e3ac
commit
94fc50e272
9 changed files with 315 additions and 105 deletions
|
@ -3,23 +3,25 @@ package usecase
|
|||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"git.sch9.ru/new_gate/ms-tester/internal/models"
|
||||
"git.sch9.ru/new_gate/ms-tester/internal/tester"
|
||||
"git.sch9.ru/new_gate/ms-tester/pkg"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type ProblemUseCase struct {
|
||||
problemRepo tester.ProblemPostgresRepository
|
||||
//pandocClient pkg.PandocClient
|
||||
problemRepo tester.ProblemPostgresRepository
|
||||
pandocClient pkg.PandocClient
|
||||
}
|
||||
|
||||
func NewProblemUseCase(
|
||||
problemRepo tester.ProblemPostgresRepository,
|
||||
// pandocClient pkg.PandocClient,
|
||||
pandocClient pkg.PandocClient,
|
||||
) *ProblemUseCase {
|
||||
return &ProblemUseCase{
|
||||
problemRepo: problemRepo,
|
||||
//pandocClient: pandocClient,
|
||||
problemRepo: problemRepo,
|
||||
pandocClient: pandocClient,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -45,14 +47,82 @@ func isEmpty(p models.ProblemUpdate) bool {
|
|||
p.InputFormat == nil &&
|
||||
p.OutputFormat == nil &&
|
||||
p.Notes == nil &&
|
||||
p.Tutorial == nil &&
|
||||
p.LatexSummary == nil &&
|
||||
p.Scoring == nil &&
|
||||
p.MemoryLimit == nil &&
|
||||
p.TimeLimit == nil
|
||||
}
|
||||
|
||||
func build(p models.ProblemStatement) string {
|
||||
return ""
|
||||
const heading = `
|
||||
\newcommand{\InputFile}{\subsection*{Входные данные}}
|
||||
\newcommand{\OutputFile}{\subsection*{Выходные данные}}
|
||||
\newcommand{\Scoring}{\subsection*{Система оценки}}
|
||||
\newcommand{\Note}{\subsection*{Примечание}}
|
||||
\newcommand{\Examples}{\subsection*{Примеры}}
|
||||
`
|
||||
|
||||
func wrap(s string) string {
|
||||
return fmt.Sprintf("%s\n\\begin{document}\n%s\n\\end{document}\n", heading, s)
|
||||
}
|
||||
|
||||
func trimSpaces(statement models.ProblemStatement) models.ProblemStatement {
|
||||
return models.ProblemStatement{
|
||||
Legend: strings.TrimSpace(statement.Legend),
|
||||
InputFormat: strings.TrimSpace(statement.InputFormat),
|
||||
OutputFormat: strings.TrimSpace(statement.OutputFormat),
|
||||
Notes: strings.TrimSpace(statement.Notes),
|
||||
Scoring: strings.TrimSpace(statement.Scoring),
|
||||
}
|
||||
}
|
||||
|
||||
func build(ctx context.Context, pandocClient pkg.PandocClient, p models.ProblemStatement) (models.Html5ProblemStatement, error) {
|
||||
p = trimSpaces(p)
|
||||
|
||||
latex := models.ProblemStatement{}
|
||||
|
||||
if p.Legend != "" {
|
||||
latex.Legend = wrap(fmt.Sprintf("\\InputFile\n%s\n", p.Legend))
|
||||
}
|
||||
|
||||
if p.InputFormat != "" {
|
||||
latex.InputFormat = wrap(fmt.Sprintf("\\InputFile\n%s\n", p.InputFormat))
|
||||
}
|
||||
|
||||
if p.OutputFormat != "" {
|
||||
latex.OutputFormat = wrap(fmt.Sprintf("\\OutputFile\n%s\n", p.OutputFormat))
|
||||
}
|
||||
|
||||
if p.Notes != "" {
|
||||
latex.Notes = wrap(fmt.Sprintf("\\Note\n%s\n", p.Notes))
|
||||
}
|
||||
|
||||
if p.Scoring != "" {
|
||||
latex.Scoring = wrap(fmt.Sprintf("\\Scoring\n%s\n", p.Scoring))
|
||||
}
|
||||
|
||||
req := []string{
|
||||
latex.Legend,
|
||||
latex.InputFormat,
|
||||
latex.OutputFormat,
|
||||
latex.Notes,
|
||||
latex.Scoring,
|
||||
}
|
||||
|
||||
res, err := pandocClient.BatchConvertLatexToHtml5(ctx, req)
|
||||
if err != nil {
|
||||
return models.Html5ProblemStatement{}, err
|
||||
}
|
||||
|
||||
if len(res) != len(req) {
|
||||
return models.Html5ProblemStatement{}, fmt.Errorf("wrong number of fieilds returned: %d", len(res))
|
||||
}
|
||||
|
||||
return models.Html5ProblemStatement{
|
||||
LegendHtml: res[0],
|
||||
InputFormatHtml: res[1],
|
||||
OutputFormatHtml: res[2],
|
||||
NotesHtml: res[3],
|
||||
ScoringHtml: res[4],
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (u *ProblemUseCase) UpdateProblem(ctx context.Context, id int32, problemUpdate models.ProblemUpdate) error {
|
||||
|
@ -71,19 +141,13 @@ func (u *ProblemUseCase) UpdateProblem(ctx context.Context, id int32, problemUpd
|
|||
}
|
||||
|
||||
statement := models.ProblemStatement{
|
||||
Title: problem.Title,
|
||||
Legend: problem.Legend,
|
||||
InputFormat: problem.InputFormat,
|
||||
OutputFormat: problem.OutputFormat,
|
||||
Notes: problem.Notes,
|
||||
Tutorial: problem.Tutorial,
|
||||
TimeLimit: problem.TimeLimit,
|
||||
MemoryLimit: problem.MemoryLimit,
|
||||
Scoring: problem.Scoring,
|
||||
}
|
||||
|
||||
if problemUpdate.Title != nil {
|
||||
statement.Title = *problemUpdate.Title
|
||||
}
|
||||
if problemUpdate.Legend != nil {
|
||||
statement.Legend = *problemUpdate.Legend
|
||||
}
|
||||
|
@ -96,19 +160,29 @@ func (u *ProblemUseCase) UpdateProblem(ctx context.Context, id int32, problemUpd
|
|||
if problemUpdate.Notes != nil {
|
||||
statement.Notes = *problemUpdate.Notes
|
||||
}
|
||||
if problemUpdate.Tutorial != nil {
|
||||
statement.Tutorial = *problemUpdate.Tutorial
|
||||
}
|
||||
if problemUpdate.TimeLimit != nil {
|
||||
statement.TimeLimit = *problemUpdate.TimeLimit
|
||||
}
|
||||
if problemUpdate.MemoryLimit != nil {
|
||||
statement.MemoryLimit = *problemUpdate.MemoryLimit
|
||||
if problemUpdate.Scoring != nil {
|
||||
statement.Scoring = *problemUpdate.Scoring
|
||||
}
|
||||
|
||||
builtStatement := build(statement)
|
||||
if builtStatement != problem.LatexSummary {
|
||||
problemUpdate.LatexSummary = &builtStatement
|
||||
builtStatement, err := build(ctx, u.pandocClient, trimSpaces(statement))
|
||||
if err != nil {
|
||||
return errors.Join(err, tx.Rollback())
|
||||
}
|
||||
|
||||
if builtStatement.LegendHtml != problem.LegendHtml {
|
||||
problemUpdate.LegendHtml = &builtStatement.LegendHtml
|
||||
}
|
||||
if builtStatement.InputFormatHtml != problem.InputFormatHtml {
|
||||
problemUpdate.InputFormatHtml = &builtStatement.InputFormatHtml
|
||||
}
|
||||
if builtStatement.OutputFormatHtml != problem.OutputFormatHtml {
|
||||
problemUpdate.OutputFormatHtml = &builtStatement.OutputFormatHtml
|
||||
}
|
||||
if builtStatement.NotesHtml != problem.NotesHtml {
|
||||
problemUpdate.NotesHtml = &builtStatement.NotesHtml
|
||||
}
|
||||
if builtStatement.ScoringHtml != problem.ScoringHtml {
|
||||
problemUpdate.ScoringHtml = &builtStatement.ScoringHtml
|
||||
}
|
||||
|
||||
err = u.problemRepo.UpdateProblem(ctx, tx, id, problemUpdate)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue