LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Bash script to create a minimalist Epub with "HELLO WORLD"? (https://www.linuxquestions.org/questions/programming-9/bash-script-to-create-a-minimalist-epub-with-hello-world-4175590545/)

Xeratul 10-01-2016 06:31 AM

Bash script to create a minimalist Epub with "HELLO WORLD"?
 
Hello

Would you know a bash script to convert "hello world" into an epub.

ex:
Code:

echo "hello world" > myepub.txt
bash mytxttoepub.sh "myepub.txt"

and it'll result into helloworld.epub

an example
https://github.com/rbrito/epub-example/tree/master/EPUB


I remember that epub is just as as format ZIP file!

Herewith there is a complex way.
...
Quote:



Here's an epub 3.0 file that validates with epubcheck 3.0.1 (the current version at the time of writing). There's plenty more info that can (and should) go in there, of course, but you asked for minimums. We'll start with file structure: in the root directory, there's one mimetype file and two folders--the OEBPS and the META-INF. Here's the mimetype file contents:

application/epub+zip

be sure not to have any return/line break characters in there. In the META-INF folder, there's one file, the container.xml:

<?xml version="1.0"?>
<container version="1.0" xmlns="urn:oasis:names:tc:opendocument:xmlns:container">
<rootfiles>
<rootfile full-path="OEBPS/content.opf" media-type="application/oebps-package+xml"/>
</rootfiles>
</container>

In the OEBPS folder, things get slightly more complex. First is the content.opf file:

<?xml version="1.0" encoding="UTF-8" ?>
<package xmlns="http://www.idpf.org/2007/opf" xmlns:dc="http://purl.org/dc/elements/1.1/" unique-identifier="db-id" version="3.0">

<metadata>
<dc:title id="t1">Title</dc:title>
<dc:identifier id="db-id">isbn</dc:identifier>
<meta property="dcterms:modified">2014-03-27T09:14:09Z</meta>
<dc:language>en</dc:language>
</metadata>

<manifest>
<item id="toc" properties="nav" href="toc.xhtml" media-type="application/xhtml+xml" />
<item id="ncx" href="toc.ncx" media-type="application/x-dtbncx+xml" />
<item id="template_css" href="template.css" media-type="text/css" />
<item id="hello" href="1_hello.xhtml" media-type="application/xhtml+xml" />
</manifest>

<spine toc="ncx">
<itemref idref="hello" />
</spine>

</package>

Then comes the toc.xhtml file:

<?xml version="1.0" encoding="utf-8"?>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops">
<head>
<title>toc.xhtml</title>
<link href="template.css" rel="stylesheet" type="text/css" />
</head>

<body>

<nav id="toc" epub:type="toc">
<h1 class="frontmatter">Table of Contents</h1>
<ol class="contents">
<li><a href="1_hello.xhtml">Hello</a></li>
</ol>

</nav>

</body>

</html>

And finally we have the content of the ebook itself, in this case 1_hello.xhtml:

<?xml version="1.0" encoding="utf-8"?>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops">
<head>
<title>1_hello.xhtml</title>
<link href="template.css" rel="stylesheet" type="text/css" />
</head>

<body>

<h1>Hello World!</h1>

</body>
</html>

As a bonus, there's a toc.ncx file. This is not required to be valid, but it's required if you want the book to have a working Table of Contents on epub 2.0.1 devices (which is pretty much all of them):

<?xml version="1.0" encoding="UTF-8" ?>
<ncx version="2005-1" xml:lang="en" xmlns="http://www.daisy.org/z3986/2005/ncx/">

<head>
<meta name="dtb:uid" content="isbn"/>
<meta name="dtb:depth" content="1"/>
</head>

<docTitle>
<text></text>
</docTitle>

<navMap>
<navPoint id="hello" playOrder="1">
<navLabel><text>cover</text></navLabel>
<content src="1_hello.xhtml" />
</navPoint>
</navMap>

</ncx>

And there's also the template.css in there, which is just a CSS stylesheet. In this case, it looks like this:

h1 {
text-align: center;
}

And that's it! For more complex stuff, there are a number of free epub 3 samples available at https://code.google.com/p/epub-samples/.
shareimprove this answer

edited Apr 1 '14 at 17:02

answered Mar 27 '14 at 16:33
Tom
3,5671425



I respect the level of effort you put into your answer but you do realize it isn't a valid ePub and you should provide more detail in case someone copies to produce from. – DᴀʀᴛʜVᴀᴅᴇʀ Mar 28 '14 at 1:09


Hmm; validates for me just fine. What errors are you seeing and what version of epubcheck are you using? – Tom Mar 28 '14 at 1:11
2

ill provide an answer with clarity when I have some extra time. – DᴀʀᴛʜVᴀᴅᴇʀ Mar 28 '14 at 1:27
2

Sounds great! That's one of the things the Stack Exchange sites are best for. – Tom Mar 28 '14 at 1:59
1

@Gramps Do these retailers require derivations from resp. additions to the standard? In that case, I would not support your "should be valid across the board" claim. This thinking led us to divergent technology in HTML 4 and consorts. If they don't adhere to the standard, nobody should support them. End of story. – Raphael Mar 28 '14 at 12:43
show 4 more comments
up vote
4
down vote


In regards to my comments about Tom's ePub not validating there is an error in his toc.xhtml on line 13 and should be coded as <li><a href="1_hello.xhtml">Hello</a></li> which will produce the error message from IDPF validator as:

enter image description here

In regards to the version 3.0 ePub I disagree and believe it should be noted that when an ePub is built as ePub 3.0 format ONLY it will not validate other than 3.0 in epubcheck. This can be achieved and tested in the terminal using epubcheck command line (which is a different question).

If the provided ePub is submitted and ran against epubcheck version 2.0 it will fail for the following:

value of attribute "version" is invalid; must be equal to "2.0"
attribute "property" not allowed here; expected attribute "content", "id", "name", "scheme" or "xml:lang"
element "meta" missing required attributes "content" and "name"
text not allowed here; expected the element end-tag
attribute "properties" not allowed here; expected attribute "fallback", "fallback-style", "href", "media-type", "required-modules" or "required-namespace"
element "nav" not allowed anywhere; expected element "address", "blockquote", "del", "div", "dl", "h1", "h2", "h3", "h4", "h5", "h6", "hr", "ins", "noscript", "ns:svg", "ol", "p", "pre", "script", "table" or "ul" (with xmlns:ns="http://www.w3.org/2000/svg")
element "nav" not allowed anywhere; expected element "address", "blockquote", "del", "div", "dl", "h1", "h2", "h3", "h4", "h5", "h6", "hr", "ins", "noscript", "ns:svg", "ol", "p", "pre", "script", "table" or "ul" (with xmlns:ns="http://www.w3.org/2000/svg")

That displayed my argument with the industry standard is that a version 2.0 ePub CAN be built to support and function with devices that run version 3.0 and it should be coded as such for quality and validation.

Also, if we are talking about what the structure of ePubs are. Using a OEBPS file structure was technically meant for ePub's in version 1. Since it really doesn't matter what the file structure is as long as the container.xml points and finds a true .opf file I petition and build my ePubs like so:

META-INF
-container.xml
xhtml
-toc.xhtml
-.xhtml files
css
-stylesheet.css
content.opf
toc.ncx
mimetype

I find it easier for maintenance and less annoying searching for a .opf or .ncx file in either OEBPS, OPS, OPF, etc. etc. file structures.

Also Im a big fan of commenting my code and since it will not change the performance I code my manifest as such:

<manifest>
<!-- .ncx -->
<item id="ncx" href="toc.ncx" media-type="application/x-dtbncx+xml" />

<!-- .xhtml -->
<item id="toc" href="toc.xhtml" media-type="application/xhtml+xml" />
<item id="hello" href="1_hello.xhtml" media-type="application/xhtml+xml" />

<!-- .css -->
<item id="css_file" href="stylesheet.css" media-type="text/css" />
</manifest>

shareimprove this answer

edited Mar 31 '14 at 18:57

answered Mar 31 '14 at 18:38

grail 10-01-2016 07:00 AM

I am guessing you have searched and not found a pre-written solution.

As it sounds like a good little challenge ... what have you done to solve it? (apart from ask here to have it done)

Xeratul 10-01-2016 07:04 AM

Quote:

Originally Posted by grail (Post 5612367)
I am guessing you have searched and not found a pre-written solution.

As it sounds like a good little challenge ... what have you done to solve it? (apart from ask here to have it done)

You are so funny. Maybe you aren't capable to code in SH/BASH or even better in C/C++ to make it? ;)

I just wonder why there is nothing else than pandoc, since it is so simple to make EPUB by hand.

wpeckham 10-01-2016 08:11 AM

Quote:

Originally Posted by Xeratul (Post 5612371)
You are so funny. Maybe you aren't capable to code in SH/BASH or even better in C/C++ to make it? ;)

I just wonder why there is nothing else than pandoc, since it is so simple to make EPUB by hand.

Let me get this straight, you have ONE person who is clearly interested in your question and wants to know what you have already done, and .. Are you trying to insult him there?

We are not here to give you canned solutions. You want to solve this, we are interested in helping you get there. First, we need to know what you found, what you have tried, and how well it worked or how it did not. If you insist on being rude, you will not like the few answers.

Have you researched the epub format or searched for epub creator software?

goumba 10-01-2016 08:15 AM

Quote:

Originally Posted by Xeratul (Post 5612371)
You are so funny. Maybe you aren't capable to code in SH/BASH or even better in C/C++ to make it? ;)

I just wonder why there is nothing else than pandoc, since it is so simple to make EPUB by hand.

Being that you haven't shown any attempt, I don't think you're in a position to say that.
grail hasn't said anything he shouldn't have. You post in the forum for programming help, in which you post the code you have come up with, and where you need the help. It's not "Programming Do It For Me."

You yourself state it is so simple, and yet, you show nothing for it. It is a rather simple task. So start writing, show us where you think you went wrong, and what results you are or are not getting.

http://www.linuxquestions.org/questi...#faq_welcomelq
http://www.catb.org/%7Eesr/faqs/smar....html#courtesy

grail 10-01-2016 09:02 AM

Quote:

Originally Posted by Xeratul
Maybe you aren't capable to code in SH/BASH

Happy to let the 30+ results from a search of this site speak to my abilities.

One would think after 1600+ posts on this site that you might have learnt how to ask a question in a way that others might assist.

Good luck on your solution.

keefaz 10-01-2016 10:35 AM

Uh, use bash to convert document to xml? A good little chalenge indeed...
Unless documents are invariably following the same model, it's going to be tedious

Axel van Moorsel 10-01-2016 10:41 AM

Hi,

Interesting challenge. After some searching and testing I found a solution that seems to work. This is just a "quick and Dirty" way, where many settings are "default" (like "title", "author"). I think it is very well possible to set these in the script, but for now I wanted to keep it as simple as possible. I used your link https://github.com/rbrito/epub-example/tree/master/EPUB as the base for the files and folders, changing them just a little.

First, because the ePub uses a folder structure, I created a template structure in my home directory:
Code:

~/epub/epub template
~/epub/epub template/META-INF
~/epub/epub template/OEBPS

The space in the folder name "epub template" is intentionally. I don't like folder names with spaces but many people use them, so I wanted to make sure the script works with such folders.
In the folder "epub template" I created a text file "mimetype" with content "application/epub+zip".
In the folder "META-INF" comes a text file "container.xml" with content:
Code:

<?xml version="1.0" encoding="utf-8"?>
<container version="1.0" xmlns="urn:oasis:names:tc:opendocument:xmlns:container">
  <rootfiles>
      <rootfile full-path="OEBPS/book.opf" media-type="application/oebps-package+xml"/>
  </rootfiles>
</container>

This file i copied without changes from the link you provided.
In the folder "OEBPS" I placed two files: "book.opf" and "book.ncx". These were copied with some changes from the afore mentioned link. content of "books.opf" is:
Code:

<?xml version="1.0" encoding="utf-8"?>
<package version="2.0" xmlns="http://www.idpf.org/2007/opf"
  unique-identifier="abcdef">
  <metadata xmlns:dc="http://purl.org/dc/elements/1.1/"
    xmlns:opf="http://www.idpf.org/2007/opf">
    <dc:title>My Fisrt Epub</dc:title>
    <dc:creator opf:role="aut">Just me</dc:creator>
    <dc:language>en-US</dc:language>
    <dc:identifier id="abcdef">abcdef</dc:identifier>
  </metadata>
  <manifest>
    <item id="myepub" href="myepub.xhtml" media-type="application/xhtml+xml" />
    <item id="ncx"    href="book.ncx"    media-type="application/x-dtbncx+xml" />
  </manifest>
  <spine toc="ncx">
    <itemref idref="myepub" />
  </spine>
</package>

and content of "books.ncx" is:
Code:

<?xml version="1.0" encoding="utf-8"?>
<ncx version="2005-1" xml:lang="en" xmlns="http://www.daisy.org/z3986/2005/ncx/">
  <head>
    <meta name="dtb:uid" content="abcdef" />
    <meta name="dtb:depth" content="1" />
    <meta name="dtb:totalPageCount" content="0" />
    <meta name="dtb:maxPageNumber" content="0" />
  </head>
  <docTitle>
    <text>My Fisrt Epub</text>
  </docTitle>
  <navMap>
    <navPoint id="myepub" playOrder="1">
      <navLabel><text>My book</text></navLabel>
      <content src="myepub.xhtml"/>
    </navPoint>
  </navMap>
</ncx>

Finally the script that creates the ePub:
Code:

#!/bin/bash

# Set some Variables
EpubTemplate="~/epub/epub template"
WorkDir="/tmp/epub"
TextFile="~/epub/myepub.txt"        # Name and location of your text file
EpubFile="/ax/wrk/epub/myepub.epub"  # Name and location of where you want your ePub

# If workfolder exist on the system, then exit.
# This ensures that you always work with just the files you need.
# If you are sure this is not necessary, then omit this check.
# You also can delete workfolder if it exists.
# No need to say you should be VERY careful when deleting
# complete folder structures with a script.
if [ -d "$WorkDir" ] ; then echo "Temporary folder ($WorkDir) exists!"; exit; fi

# Copy your epub template to workfolder.
mkdir -pv "$WorkDir"
cp -r "$EpubTemplate" "$WorkDir/myepub"

# Create xhtml document file
echo "<p><pre>" > "$WorkDir/myepub/OEBPS/myepub.xhtml"
cat "$TextFile" >> "$WorkDir/myepub/OEBPS/myepub.xhtml"
echo "</pre></p>" >> "$WorkDir/myepub/OEBPS/myepub.xhtml"

# Create Epub file
7z a -tzip "$EpubFile" "$WorkDir"/myepub/mimetype
7z a -tzip "$EpubFile" "$WorkDir"/myepub/META-INF
7z a -tzip "$EpubFile" "$WorkDir"/myepub/OEBPS

# Clean up temporary folder
if [ -f "$WorkDir/myepub/OEBPS/myepub.xhtml" ] ; then rm -fr "$WorkDir"; fi
exit

I hope the comments in the script are clear.
Off course the template and the script can be modified for other needs, but I think this is a basic answer to your question.
Hope you'll enjoy using this script as much as I did making it.

Xeratul 04-08-2017 02:23 AM

Hello

I have found a way to do it with a single bash file:

use:
Code:

epub-maker.sh whateverdocumentfile.txt

Code:

#!/bin/bash



echo  =============================
echo  ==== EBOOK-BASH-CREATOR =====
echo  =============================

if [ "$1" = "" ] ; then
  exit
  break
fi


pwd
mkdir ~/epub/
mkdir -p ~/epub/epub-template
mkdir -p ~/epub/epub-template/META-INF
mkdir -p ~/epub/epub-template/OEBPS

echo "application/epub+zip" >  ~/epub/epub-template/mimetype

echo '<?xml version="1.0" encoding="utf-8"?>' >  ~/epub/epub-template/META-INF/container.xml
echo '<container version="1.0" xmlns="urn:oasis:names:tc:opendocument:xmlns:container">'  >>  ~/epub/epub-template/META-INF/container.xml
echo '  <rootfiles>'  >>  ~/epub/epub-template/META-INF/container.xml
echo '      <rootfile full-path="OEBPS/book.opf" media-type="application/oebps-package+xml"/>'  >>  ~/epub/epub-template/META-INF/container.xml
echo '  </rootfiles>'  >>  ~/epub/epub-template/META-INF/container.xml
echo '</container>'  >>  ~/epub/epub-template/META-INF/container.xml

# >> ~/epub/epub-template/OEBPS/books.opf
# ~/epub/epub-template/OEBPS/books.opf
# ~/epub/epub-template/OEBPS
#This file i copied without changes from the link you provided.
#In the folder "OEBPS" I placed two files: "book.opf" and "book.ncx". These were copied with some changes from the afore mentioned link. content of "books.opf" is:
#Code:
echo '<?xml version="1.0" encoding="utf-8"?>'  > ~/epub/epub-template/OEBPS/books.opf
echo '<package version="2.0" xmlns="http://www.idpf.org/2007/opf"'  >> ~/epub/epub-template/OEBPS/books.opf
echo '  unique-identifier="abcdef">'  >> ~/epub/epub-template/OEBPS/books.opf
echo '  <metadata xmlns:dc="http://purl.org/dc/elements/1.1/"'  >> ~/epub/epub-template/OEBPS/books.opf
echo '    xmlns:opf="http://www.idpf.org/2007/opf">'  >> ~/epub/epub-template/OEBPS/books.opf
echo '    <dc:title>My Fisrt Epub</dc:title>'  >> ~/epub/epub-template/OEBPS/books.opf
echo '    <dc:creator opf:role="aut">Just me</dc:creator>'  >> ~/epub/epub-template/OEBPS/books.opf
echo '    <dc:language>en-US</dc:language>'  >> ~/epub/epub-template/OEBPS/books.opf
echo '    <dc:identifier id="abcdef">abcdef</dc:identifier>'  >> ~/epub/epub-template/OEBPS/books.opf
echo '  </metadata>'  >> ~/epub/epub-template/OEBPS/books.opf
echo '  <manifest>'  >> ~/epub/epub-template/OEBPS/books.opf
echo '    <item id="myepub" href="myepub.xhtml" media-type="application/xhtml+xml" />'  >> ~/epub/epub-template/OEBPS/books.opf
echo '    <item id="ncx"    href="book.ncx"    media-type="application/x-dtbncx+xml" />'  >> ~/epub/epub-template/OEBPS/books.opf
echo '  </manifest>'  >> ~/epub/epub-template/OEBPS/books.opf
echo '  <spine toc="ncx">'  >> ~/epub/epub-template/OEBPS/books.opf
echo '    <itemref idref="myepub" />'  >> ~/epub/epub-template/OEBPS/books.opf
echo '  </spine>'  >> ~/epub/epub-template/OEBPS/books.opf
echo '</package>'  >> ~/epub/epub-template/OEBPS/books.opf



#and content of "books.ncx" is:
#Code:
echo '<?xml version="1.0" encoding="utf-8"?>'  > ~/epub/epub-template/OEBPS/books.ncx
echo '<ncx version="2005-1" xml:lang="en" xmlns="http://www.daisy.org/z3986/2005/ncx/">'  >> ~/epub/epub-template/OEBPS/books.ncx
echo '  <head>'  >> ~/epub/epub-template/OEBPS/books.ncx
echo '    <meta name="dtb:uid" content="abcdef" />'  >> ~/epub/epub-template/OEBPS/books.ncx
echo '    <meta name="dtb:depth" content="1" />'  >> ~/epub/epub-template/OEBPS/books.ncx
echo '    <meta name="dtb:totalPageCount" content="0" />'  >> ~/epub/epub-template/OEBPS/books.ncx
echo '    <meta name="dtb:maxPageNumber" content="0" />'  >> ~/epub/epub-template/OEBPS/books.ncx
echo '  </head>'  >> ~/epub/epub-template/OEBPS/books.ncx
echo '  <docTitle>'  >> ~/epub/epub-template/OEBPS/books.ncx
echo '    <text>My Fisrt Epub</text>'  >> ~/epub/epub-template/OEBPS/books.ncx
echo '  </docTitle>'  >> ~/epub/epub-template/OEBPS/books.ncx
echo '  <navMap>'  >> ~/epub/epub-template/OEBPS/books.ncx
echo '    <navPoint id="myepub" playOrder="1">'  >> ~/epub/epub-template/OEBPS/books.ncx
echo '      <navLabel><text>My book</text></navLabel>'  >> ~/epub/epub-template/OEBPS/books.ncx
echo '      <content src="myepub.xhtml"/>'  >> ~/epub/epub-template/OEBPS/books.ncx
echo '    </navPoint>'  >> ~/epub/epub-template/OEBPS/books.ncx
echo '  </navMap>'  >> ~/epub/epub-template/OEBPS/books.ncx
echo '</ncx>'  >> ~/epub/epub-template/OEBPS/books.ncx



echo Set some variables

# Set some Variables
EpubTemplate="$HOME/epub/epub-template"
rm -rf /tmp/epub
WorkDir="/tmp/epub"
mkdir -p "$WorkDir"
TextFile="$1"        # Name and location of your text file
EpubFile="$HOME/epub/book.epub"  # Name and location of where you want your ePub
mkdir "$HOME/epub/"


# If workfolder exist on the system, then exit.
# This ensures that you always work with just the files you need.
# If you are sure this is not necessary, then omit this check.
# You also can delete workfolder if it exists.
# No need to say you should be VERY careful when deleting
# complete folder structures with a script.
# if [ -d "$WorkDir" ] ; then echo "Temporary folder ($WorkDir) exists!"; exit; fi

# Copy your epub template to workfolder.
mkdir -pv "$WorkDir"
mkdir -p  "$WorkDir"

# Create xhtml document file
echo "Working with file $1 as input data text"
[ -f "$1" ] && echo File Found
mkdir -p    "$WorkDir/myepub/OEBPS/"
echo    "$WorkDir/myepub/OEBPS/"
echo "Use Cat of $TextFile"

echo "<p><pre>" >    "$WorkDir/myepub/OEBPS/myepub.xhtml"
cat "$TextFile" >>  "$WorkDir/myepub/OEBPS/myepub.xhtml"
echo "</pre></p>" >> "$WorkDir/myepub/OEBPS/myepub.xhtml"
ls -ltra  "$WorkDir/myepub/OEBPS/"


cd
cd "$WorkDir"/myepub/
rm -rf "/tmp/epub/myepub/
mkdir -p "/tmp/epub/myepub/

echo "Workdir $WorkDir"
cp -r "$EpubTemplate"/*  "$WorkDir/myepub"
echo  "Copy $EpubTemplate to $WorkDir/myepub"


# Create Epub file
#7zr a -tzip "$EpubFile" "$WorkDir"/myepub/mimetype
#7zr a -tzip "$EpubFile" "$WorkDir"/myepub/META-INF
#7zr a -tzip "$EpubFile" "$WorkDir"/myepub/OEBPS




pwd
 zip -rX "$EpubFile"  mimetype META-INF/ OEBPS/
 ls -ltra "$EpubFile"

echo "Working with the files $EpubFile" "$WorkDir"/myepub/OEBPS

pwd
echo "File Epub: $EpubFile done."


# Clean up temporary folder
# if [ -f "$WorkDir/myepub/OEBPS/myepub.xhtml" ] ; then rm -fr "$WorkDir"; fi
exit



the pure bash script above is not necessarily working, but thiss one
works : https://github.com/spartrekus/epub-linux-portable-maker

ondoho 04-08-2017 04:56 AM

^ just whack all text in between a <pre></pre> block, haha, that's funny.
sure, it is some kind of "solution"...
and another "in your face" to bloatware like pandoc and incapable people like grail. (*)
Xeratul, you must be so happy right now.

(*) sarcasm! do not take seriously!


All times are GMT -5. The time now is 11:40 AM.