Advertisement
banovski

Typographer

Aug 19th, 2022 (edited)
968
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.72 KB | Writing | 0 0
  1. #!/bin/bash
  2.  
  3. # The script replaces some keyboard characters and their combinations
  4. # with typographically correct ones, for example hyphens padded with
  5. # spaces are converted to em- or en-dashes. Works with both English
  6. # and Russian text. The text for conversion should be placed in the
  7. # clipboard; the converted text is returned by the script to the
  8. # clipboard too. espeak, xclip and yad need to be installed for the
  9. # script to work, otherwise they should be replaced with alternatives;
  10. # espeak is only used for verbal notification so it can be removed
  11. # from the code if such notification is not needed.
  12.  
  13. function quitting()
  14. {
  15.     espeak -v english "$1"
  16.     exit "$2"
  17. }
  18.  
  19. function cyrillic()
  20. {
  21.     echo -n "$input" | sed -r 's/(^| )([^ A-Za-zА-Яа-я0-9]*)"([^ A-Za-zА-Яа-я0-9]*)"([^ A-Za-zА-Яа-я0-9]*)([A-Za-zА-Яа-я0-9])/\1\2«\3«\4\5/g; s/([A-Za-zА-Яа-я0-9])([^ A-Za-zА-Яа-я0-9]*)"([^ A-Za-zА-Яа-я0-9]*)"([^ A-Za-zА-Яа-я0-9]*)( |$)/\1\2»\3»\4\5/g; s/(^| )([^ A-Za-zА-Яа-я0-9]*)"([^ A-Za-zА-Яа-я0-9]*)([A-Za-zА-Яа-я0-9])/\1\2«\3\4/g; s/([A-Za-zА-Яа-я0-9])([^ A-Za-zА-Яа-я0-9]*)"([^ A-Za-zА-Яа-я0-9]*)( |$)/\1\2»\3\4/g; s/^([^«»]*)«([^«»]*)«/\1„\2«/g; s/»([^«»]*)»([^«»]*)$/»\1“\2/g; s/^-+ /— /g; s/ -+ / — /g; s/\.\.\./…/g; s/([0-9]) *-+ *([0-9])/\1—\2/g' | xclip -i -selection clipboard
  22. }
  23.  
  24. function latin()
  25. {
  26.     echo -n "$input" | sed -r 's/(^| )([^ A-Za-zА-Яа-я0-9]*)"([^ A-Za-zА-Яа-я0-9]*)"([^ A-Za-zА-Яа-я0-9]*)([A-Za-zА-Яа-я0-9])/\1\2“\3“\4\5/g; s/([A-Za-zА-Яа-я0-9])([^ A-Za-zА-Яа-я0-9]*)"([^ A-Za-zА-Яа-я0-9]*)"([^ A-Za-zА-Яа-я0-9]*)( |$)/\1\2”\3”\4\5/g; s/(^| )([^ A-Za-zА-Яа-я0-9]*)"([^ A-Za-zА-Яа-я0-9]*)([A-Za-zА-Яа-я0-9])/\1\2“\3\4/g; s/([A-Za-zА-Яа-я0-9])([^ A-Za-zА-Яа-я0-9]*)"([^ A-Za-zА-Яа-я0-9]*)( |$)/\1\2”\3\4/g; s/^([^“”]*)“([^“”]*)“/\1‘\2“/g; s/”([^“”]*)”([^“”]*)$/”\1’\2/g; s/^-+ /– /g; s/ -+ / – /g; s/\.\.\./…/g; s/([0-9]) *-+ *([0-9])/\1–\2/g' | xclip -i -selection clipboard
  27. }
  28.  
  29. input=$(xclip -o -selection clipboard)
  30.  
  31. if [[ $input =~ ^.*[A-Za-z].*[А-Яа-я].*$ ]] || [[ $input =~ ^.*[А-Яа-я].*[A-Za-z].*$ ]]; then
  32.  
  33.     yad --title="Typograph" --text="Select a character set: " --button='Cyrillic:0' --button='Latin:1' --button="Abort:2" --borders=8
  34.     case "$?" in
  35.         0)
  36.             cyrillic
  37.             ;;
  38.         1)
  39.             latin
  40.             ;;
  41.         2)
  42.             quitting "Aborted!" 1
  43.             ;;
  44.         *)
  45.             true
  46.             ;;
  47.     esac
  48. else
  49.     if [[ $input =~ ^.*[А-Яа-я].*$ ]]; then
  50.         cyrillic
  51.     else
  52.         latin
  53.     fi
  54. fi
  55.  
  56. quitting "Done!" 0
  57.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement