Bash script to create a minimalist Epub with "HELLO WORLD"?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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:
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):
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:
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?
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.
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
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:
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:
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:
#!/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.
^ 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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.