aboutsummaryrefslogblamecommitdiff
path: root/scripts/assert-content.sh
blob: a832a238fd10be519eb0104a9206b917490d92e9 (plain) (tree)
1
2
3
4
5
6
7
8
9
10


                   



                                     


                       
                     
                      
 
                                 

                                            

                              



                                   
                      
                             
                             
                            


                     













                                                    








                                             

                                                                   


            
                                                                 

 



                                               



                                                

             
 
                      


                     
                      
                          

                       

                             
                                                  
                                                 
 




                                                            

                                                      
          
    
 


                                                                         
          

    

                                            
                                                
                                                              

                           
                                                             

            
 
                                                                           






                                              




                                                                                  
      




                                                             
                                                   


                         
                                                       
                          
                                                            
                                       
    

    

                                                               
                                                         

    

                                                     
                                           

    

                                                         
                                                          

    

                                 
                     
           


                                                              
                                 
                              
                                  








                                                            
      

      
 



                                                                        
 
              
#!/usr/bin/env bash
set -Eeuo pipefail

end="\033[0m"
red="\033[0;31m"
red() { echo -e "${red}${1}${end}"; }


## Constant definitions

jekyll build --future
JSON='_site/site.json'

LANGS=(en pt fr eo) # jp zh es de
IGNORED_PAGES=(site.json sitemap.xml *.atom)

## Helper function definitions

slugify() {
  echo "${1}"                   | \
    tr '[:upper:]' '[:lower:]'  | \
    perl -ne 'tr/\000-\177//cd;
              s|/|-|g;
              s/[^\w\s-.]//g;
              s/^\s+|\s+$//g;
              s/[-\s.]+/-/g;
              print;'
}

contains-element() {
  local e match="$1"
  shift
  for e; do [[ "$e" == "$match" ]] && return 0; done
  return 1
}

fail-attr() {
  ATTRIBUTE="${1}"
  URL="${2}"
  red "Undefined '${ATTRIBUTE}' for ${URL}." >&2
  exit 1
}

get-lang() {
  echo "${1}" | base64 --decode | jq -r .lang
}

get-ref() {
  echo "${1}"  | base64 --decode | jq -r .ref
}

get-url() {
  # Remove leading / to match more closely the filesystem hierarchy
  echo "${1}"  | base64 --decode | jq -r .url | sed 's_^/__'
}

get-date() {
  echo "${1}"  | base64 --decode | jq -r .date | awk '{print $1}'
}

get-title() {
  echo "${1}"  | base64 --decode | jq -r .title
}

get-layout() {
  echo "${1}"  | base64 --decode | jq -r .layout
}


## Assertions

assert-frontmatter() {
  F="$1"
  DESIRED_LAYOUT="$2"
  PREFIX="${3:-}"
  EXTENSION="${4:-md}"
  LLANG="$(get-lang "$F")"
  REF="$(get-ref "$F")"
  URL="$(get-url "$F")"
  LAYOUT="$(get-layout "$F")"

  [[ -z "${LLANG}" ]] && fail-attr 'lang' "${URL}"
  [[ -z "${REF}" ]]  && fail-attr 'ref'  "${URL}"

  if ! contains-element "${URL}" "${IGNORED_PAGES[@]}"; then
    TITLE="$(get-title "$F")"
    [[ -z "${TITLE}" ]]  && fail-attr 'title' "${URL}"
  fi

  if ! contains-element "${LLANG}" "${LANGS[@]}"; then
    red "Invalid lang '${LLANG}' in ${URL}." >&2
    exit 1
  fi

  if [[ "${DESIRED_LAYOUT}" != "${LAYOUT}" ]]; then
    red "Layout mismatch: expected '${DESIRED_LAYOUT}', got '${LAYOUT}'."
    red "Page: ${URL}."
    exit 1
  fi

  if [[ "$DESIRED_LAYOUT" != 'page' ]]; then
    DATE="$(get-date "$F")"
    URL_BASENAME="$(basename "$(get-url "$F")")"
    FILE="${PREFIX}${DATE}-${URL_BASENAME%.html}.${EXTENSION}"

    [[ -f "${FILE}" ]] || {
      red "date/filename mismatch: '${FILE}' does not exist."
      exit 1
    }

    if [[ "$DESIRED_LAYOUT" != 'pastebin' ]] && [[ "$LLANG" = 'en' ]]; then
      TITLE_SLUG="$(slugify "$TITLE")"
      if [[ "$TITLE_SLUG" != "$REF" ]]; then
        red "ref isn't the slug of the title."
        red "ref:        '$REF'"
        red "title slug: '$TITLE_SLUG'"
        exit 1
      fi
      DESIRED_FILE="${PREFIX}${DATE}-${TITLE_SLUG}.${EXTENSION}"
      if [[ ! -f "$DESIRED_FILE" ]]; then
        red "File can't be guessed from date+slug: '$DESIRED_FILE' does not exist"
        exit 1
      fi
    fi
  fi
}

echo Linting articles... >&2
for article in $(jq -r '.articles[] | @base64' "${JSON}"); do
  assert-frontmatter "$article" 'post' '_articles/'
done

echo Linting pages... >&2
for page in $(jq -r '.pages[] | @base64' "${JSON}"); do
  URL="$(get-url "$page")"
  if ! contains-element "${URL}" "${IGNORED_PAGES[@]}"; then
    assert-frontmatter "${page}" 'page'
  fi
done

echo Linting pastebins... >&2
for pastebin in $(jq -r '.pastebins[] | @base64' "${JSON}"); do
  assert-frontmatter "$pastebin" 'pastebin' '_pastebins/'
done

echo Linting tils... >&2
for til in $(jq -r '.tils[] | @base64' "${JSON}"); do
  assert-frontmatter "$til" 'post' '_tils/'
done

echo Linting slides... >&2
for slide in $(jq -r '.slides[] | @base64' "${JSON}"); do
  assert-frontmatter "$slide" 'slides' '_slides/' 'slides'
done

echo Asserting unique refs... >&2
KNOWN_IDS=()
assert-unique-ref() {
  TYPE="$2"
  for page in $1; do
    URL="$(get-url "$page")"
    if ! contains-element "${URL}" "${IGNORED_PAGES[@]}"; then
      LLANG="$(get-lang "$page")"
      REF="$(get-ref "$page")"
      ID="${TYPE}:${LLANG}:${REF}"

      if contains-element "${ID}" "${KNOWN_IDS[@]}"; then
        printf '%s\n' "${KNOWN_IDS[@]}"
        red "Duplicated lang:ref match: '${ID}'." >&2
        red "Page: ${URL}." >&2
        exit 1
      fi

      KNOWN_IDS+=("${ID}") # printf '%s\n' "${KNOWN_IDS[@]}"
    fi
  done
}

assert-unique-ref "$(jq -r    '.pages[] | @base64' "${JSON}")" 'page'
assert-unique-ref "$(jq -r '.articles[] | @base64' "${JSON}")" 'article'
assert-unique-ref "$(jq -r     '.tils[] | @base64' "${JSON}")" 'til'
assert-unique-ref "$(jq -r   '.slides[] | @base64' "${JSON}")" 'slides'

echo Done. >&2