Advertisement
Guest User

split-masto-text

a guest
Jul 18th, 2019
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Awk 1.58 KB | None | 0 0
  1. #!/usr/bin/gawk -f
  2. # split-masto-text
  3. #
  4. # ----------------------------------------------------------------------
  5. # Split input text into multiple chunks, based on Mastodon's maximum
  6. # message size, with element numbers.  Useful with Craig Maloney's
  7. # Tootstream
  8. #
  9. # Preserve whitespace and line breaks.
  10. # Do not split words
  11.  
  12. # Separate chunks with "...\n\n#<nn>/"
  13. #
  14. # Note that in replies, Tootstream adds the user handles of other
  15. # participants in the discussion, so headway may be _less_ than 500
  16. # characters.  That's the 'extra' parameter here.  It may need to be
  17. # finagled.
  18.  
  19. BEGIN {
  20.     maxlen = 500  # Maximum mastodon toot size
  21.     extra = 20    # Additional housekeeping, also need to add chunk_no width
  22.    
  23.  
  24.     # Initialize
  25.     chunk_no = 1
  26.     chunk_size = 0
  27.  
  28.     while( getline ) {
  29.  
  30.     outfile = FILENAME "-" chunk_no
  31.  
  32.         # chunk_size + line < 500?  output.
  33.     if( (chunk_size + length($0) + extra + length(chunk_no)) >= maxlen )  {
  34.  
  35.         # Walk through the current line until we reach max size
  36.         for( i=1; i<=NF; i++ ) {
  37.         # Reached your limit?
  38.  
  39.         curlen = chunk_size + length($i) + 1 + extra + length(chunk_no)
  40.             if( curlen > maxlen ) {
  41.             # Close out the current file:
  42.             printf( "...\n\n%s/\n", chunk_no) >> outfile
  43.             close(outfile)
  44.             chunk_size = 0
  45.             chunk_no++
  46.             outfile = FILENAME "-" chunk_no
  47.         }
  48.  
  49.         # Unconditionally
  50.         chunk_size += 1 + length($i)
  51.         printf( "%s ", $i ) >> outfile
  52.         }
  53.         printf( "\n" ) >> outfile
  54.     }
  55.  
  56.     else {
  57.         chunk_size += length($0) + 1
  58.         print >> outfile
  59.     }
  60.     }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement