Skip to content

Commit

Permalink
add CSV input format support
Browse files Browse the repository at this point in the history
  • Loading branch information
clash82 committed Nov 24, 2023
1 parent 42b5e67 commit ee36fed
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 89 deletions.
20 changes: 12 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ This conversion tool allows you to convert Bible translation in one of the suppo

Supported input formats are:

Name | Id | File extension
-------------------|---------|---------------
eSword | sword | *.bblx, *.mybible
Zefania | zefania | *.xml
theWord | word | *.ot, *.nt, *.ont
tab as a separator | tsv | *.tsv
MyBible (Android) | mybible | *.SQLite3
Name | Id | File extension
---------------------|---------|---------------
eSword | sword | *.bblx, *.mybible
Zefania | zefania | *.xml
theWord | word | *.ot, *.nt, *.ont
tab as a separator | tsv | *.tsv
comma as a separator | csv | *.csv
MyBible (Android) | mybible | *.SQLite3

Compilation steps:
-------------------
Expand Down Expand Up @@ -46,6 +47,9 @@ php bib2xml.phar convert input_translation.bblx
Changelog:
----------

* v0.6.0.0 (2023-11-24):
- added `csv` input format support

* v0.5.0.0 (2022-03-17):
- upgraded PHP to v8
- added `mybible` input format support
Expand Down Expand Up @@ -75,7 +79,7 @@ Changelog:
About
-----

Original author: [Rafał Toborek](https://kontakt.toborek.info)
Author: [Rafał Toborek](https://kontakt.toborek.info)

Feel free to contribute and [support my work](https://rbiblia.toborek.info/donation/).

Expand Down
4 changes: 4 additions & 0 deletions project/src/Command/ConvertCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ class ConvertCommand extends Command
'name' => 'TSV (tab as a separator)',
'ext' => ['tsv'],
],
'csv' => [
'name' => 'CSV (comma as a separator)',
'ext' => ['csv'],
],
'mybible' => [
'name' => 'MyBible',
'ext' => ['sqlite3'],
Expand Down
14 changes: 14 additions & 0 deletions project/src/Input/CsvInput.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

namespace rBibliaBibleConverter\Input;

use rBibliaBibleConverter\Input\Traits\CharSeparatedTrait;

class CsvInput implements InputConverter
{
use CharSeparatedTrait;

private string $separator = ';';
}
107 changes: 107 additions & 0 deletions project/src/Input/Traits/CharSeparatedTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?php

declare(strict_types=1);

namespace rBibliaBibleConverter\Input\Traits;

use rBibliaBibleConverter\Bible\Books;
use rBibliaBibleConverter\Reader\InputReader;
use rBibliaBibleConverter\Text\Sanitizer;
use rBibliaBibleConverter\Value\About;
use rBibliaBibleConverter\Value\Book;
use rBibliaBibleConverter\Value\Chapter;
use rBibliaBibleConverter\Value\Translation;
use rBibliaBibleConverter\Value\Verse;

trait CharSeparatedTrait
{
/** @var InputReader */
private $input;

/** @var Sanitizer */
private $sanitizer;

/** @var Books */
private $books;

public function __construct(InputReader $input, Sanitizer $sanitizer, Books $books)
{
$this->input = $input;
$this->sanitizer = $sanitizer;
$this->books = $books;
}

public function getTranslation(): Translation
{
$translationArray = [];

$content = explode(\PHP_EOL, $this->input->getContent());
foreach ($content as $line) {
if (empty(trim($line))) {
continue;
}

$verseAsArray = explode($this->separator, $line);

$bookId = $verseAsArray[0];
$chapterId = $verseAsArray[1];
$verseId = $verseAsArray[2];

// more than one separator could appear in verse body
$verse = '';
$count = count($verseAsArray);
for ($i = 3; $i < $count; $i++) {
$verse .= $verseAsArray[$i];

if ($i < $count-1) {
$verse .= $this->separator;
}
}

if (!isset($translationArray[$bookId])) {
$translationArray[$bookId] = [];
}

if (!isset($translationArray[$bookId][$chapterId])) {
$translationArray[$bookId][$chapterId] = [];
}

if (!isset($translationArray[$bookId][$chapterId][$verseId])) {
$translationArray[$bookId][$chapterId][$verseId] = trim(str_replace(['\f6 ', '&ndash;', '&mdash;'], ['', '-', '-'], $verse));
}
}

$translation = new Translation($this->getAbout());

foreach ($translationArray as $bookId => $books) {
$book = new Book($this->books->convertNameToOneOfTheSupportedBooksId((string) $bookId));

foreach ($books as $chapterId => $chapters) {
$chapter = new Chapter($chapterId);

foreach ($chapters as $verseId => $content) {
$chapter->addVerse(new Verse($verseId, $content));
}

$book->addChapter($chapter);
}

$translation->addBook($book);
}

return $translation;
}

private function getAbout(): About
{
return new About(
'',
'',
'',
'',
'',
'',
''
);
}
}
84 changes: 3 additions & 81 deletions project/src/Input/TsvInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,89 +4,11 @@

namespace rBibliaBibleConverter\Input;

use rBibliaBibleConverter\Bible\Books;
use rBibliaBibleConverter\Reader\InputReader;
use rBibliaBibleConverter\Text\Sanitizer;
use rBibliaBibleConverter\Value\About;
use rBibliaBibleConverter\Value\Book;
use rBibliaBibleConverter\Value\Chapter;
use rBibliaBibleConverter\Value\Translation;
use rBibliaBibleConverter\Value\Verse;
use rBibliaBibleConverter\Input\Traits\CharSeparatedTrait;

class TsvInput implements InputConverter
{
/** @var InputReader */
private $input;
use CharSeparatedTrait;

/** @var Sanitizer */
private $sanitizer;

/** @var Books */
private $books;

public function __construct(InputReader $input, Sanitizer $sanitizer, Books $books)
{
$this->input = $input;
$this->sanitizer = $sanitizer;
$this->books = $books;
}

public function getTranslation(): Translation
{
$translationArray = [];

$content = explode(\PHP_EOL, $this->input->getContent());
foreach ($content as $line) {
if (empty(trim($line))) {
continue;
}

[$bookId, $chapterId, $verseId, $verse] = explode("\t", $line);

if (!isset($translationArray[$bookId])) {
$translationArray[$bookId] = [];
}

if (!isset($translationArray[$bookId][$chapterId])) {
$translationArray[$bookId][$chapterId] = [];
}

if (!isset($translationArray[$bookId][$chapterId][$verseId])) {
$translationArray[$bookId][$chapterId][$verseId] = trim(str_replace(['\f6 ', '&ndash;', '&mdash;'], ['', '-', '-'], $verse));
}
}

$translation = new Translation($this->getAbout());

foreach ($translationArray as $bookId => $books) {
$book = new Book($this->books->convertNameToOneOfTheSupportedBooksId((string) $bookId));

foreach ($books as $chapterId => $chapters) {
$chapter = new Chapter($chapterId);

foreach ($chapters as $verseId => $content) {
$chapter->addVerse(new Verse($verseId, $content));
}

$book->addChapter($chapter);
}

$translation->addBook($book);
}

return $translation;
}

private function getAbout(): About
{
return new About(
'',
'',
'',
'',
'',
'',
''
);
}
private string $separator = "\t";
}

0 comments on commit ee36fed

Please sign in to comment.