Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/gawk -f
- # split-masto-text
- #
- # ----------------------------------------------------------------------
- # Split input text into multiple chunks, based on Mastodon's maximum
- # message size, with element numbers. Useful with Craig Maloney's
- # Tootstream
- #
- # Preserve whitespace and line breaks.
- # Do not split words
- # Separate chunks with "...\n\n#<nn>/"
- #
- # Note that in replies, Tootstream adds the user handles of other
- # participants in the discussion, so headway may be _less_ than 500
- # characters. That's the 'extra' parameter here. It may need to be
- # finagled.
- BEGIN {
- maxlen = 500 # Maximum mastodon toot size
- extra = 20 # Additional housekeeping, also need to add chunk_no width
- # Initialize
- chunk_no = 1
- chunk_size = 0
- while( getline ) {
- outfile = FILENAME "-" chunk_no
- # chunk_size + line < 500? output.
- if( (chunk_size + length($0) + extra + length(chunk_no)) >= maxlen ) {
- # Walk through the current line until we reach max size
- for( i=1; i<=NF; i++ ) {
- # Reached your limit?
- curlen = chunk_size + length($i) + 1 + extra + length(chunk_no)
- if( curlen > maxlen ) {
- # Close out the current file:
- printf( "...\n\n%s/\n", chunk_no) >> outfile
- close(outfile)
- chunk_size = 0
- chunk_no++
- outfile = FILENAME "-" chunk_no
- }
- # Unconditionally
- chunk_size += 1 + length($i)
- printf( "%s ", $i ) >> outfile
- }
- printf( "\n" ) >> outfile
- }
- else {
- chunk_size += length($0) + 1
- print >> outfile
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement