📓 HOWTO: Paginate a Long Text (for example a book in a text file)

This method can be used to break up a very long text into pages for sequential viewing in an app. It respects words and does not split any words in two.

Book formats come in all shapes, sizes, formats and layouts, there is going to be some significant manual work required for most, to get them into shape, even the ones that come from Project Gutenberg in txt format.

For this example I used 10 paragraphs of lorem ipsum, but I still had to edit the paragraph double line returns and type in \n\n so that the content could be placed in a text block. It is possible to load text in from a text file which will retain the line returns, but you will still need to rename/replace them. In the blocks, I used 'space'||'space' to replace \n\n, then switched this back after conversion to pages. Getting your basic formatting correct, and the content into text format is the key. I would suggest that you break a book out into chapters or sections, rather than trying to do a whole book in one go. I also had a go at the Iliad by Homer, which needed considerable work (I ended up only trying Book1 :wink: )

I actually used a javascript function I found on StackOverflow as the basis for my blocks:

function splitString (n,str){
    let arr = str?.split(' ');
    let result=[]
    let subStr=arr[0]
    for(let i = 1; i < arr.length; i++){
        let word = arr[i]
        if(subStr.length + word.length + 1 <= n){
            subStr = subStr + ' ' + word
        }
        else{
            result.push(subStr);
            subStr = word
        }
    }
    if(subStr.length){result.push(subStr)}
    return result
}

so credits due to the author there.

Here is the resultant procedure that does all the work:

Draggable Procedure

blocks (10)

Attached here is an example aia project you can try out:
Paginate.aia (7.8 KB)

and a short video to demonstrate:

8 Likes

You gave me a great idea from this tutorial.
Thank you so much @TIMAI2 :star_struck:

thank you very much

Tim ti faccio i complimenti per il tuo grande lavoro.
Saluti
Nicola

Thank you @TIMAI2
What if we need to show the book pages directly without asking user about number of words or pages?

Then set that variable inside your app.

1 Like

Thank you @TIMAI2