Skip to content

Instantly share code, notes, and snippets.

@oganm
Last active February 22, 2018 02:16
Show Gist options
  • Save oganm/f8fc5fea789aa2a6a436dcbe51198006 to your computer and use it in GitHub Desktop.
Save oganm/f8fc5fea789aa2a6a436dcbe51198006 to your computer and use it in GitHub Desktop.
Editing grimoire
library(stringr)
library(magrittr)
library(pdftools)
library(Hmisc)
capitalizeAll = function(x){
words = strsplit(x, ' ') %>% {.[[1]]}
capital = sapply(words, function(x){
if(x %in% c('for','of','and','to','into','with','the','from','via')){
return(x)
} else{
capitalize(x)
}})
paste(capital,collapse =' ')
}
system('svn checkout https://github.com/eepMoody/open5e/trunk/source/Spellcasting/spells_a-z/')
download.file('http://media.wizards.com/2016/downloads/DND/SRD-OGL_V5.1.pdf',destfile = 'SRD.pdf')
srd = pdf_text('SRD.pdf') %>% str_replace_all('\t\r','') %>%
str_replace_all('\\s\\s',' ') %>%
str_replace_all('\n','')
# remove pages that are not part of the spell list to reduce ambiguities
srd[1:113] = ''
srd[195:403] = ''
SRDspells = list.files('spells_a-z/',recursive = TRUE) %>%
basename() %>%
str_replace(pattern = '.rst','') %>%
str_replace_all('-',' ')
SRDspells = SRDspells[!SRDspells %in% 'index']
addTag = function(spellName, tagType = c('tags','sources'), tagToAdd,spellDir = '_posts'){
# there are currently two types of tags. Ensure input is one of them
# can be removed without breaking anything
tagType = match.arg(tagType)
spellName %<>% tolower() %>% stringr::str_replace_all(' ','-')
grimoireSpells = list.files(spellDir,full.names = TRUE)
# this ensures we are matching the full name
whichSpell = grimoireSpells[grepl(pattern = paste0('[0-9]-',spellName,'.(markdown|md)'),
grimoireSpells)]
if(length(whichSpell) == 0){
print(paste(spellName,'was not found'))
return(FALSE)
}
spellLines = readLines(whichSpell)
# get which line marks the tag type you want to edit
tagZone = spellLines %>% grepl('---',x = .) %>% which
tagLine = spellLines[tagZone[1]:tagZone[2]] %>% grepl(tagType, x = .) %>% which
# add the tag to the end. doesn't care about style guides
spellLines[tagLine] %<>% str_replace('\\]',paste0(', ',tagToAdd,']'))
writeLines(spellLines,whichSpell)
return(TRUE)
}
SRDspells %>% sapply(function(x){
pageNo = which(srd %>% grepl(pattern = capitalizeAll(x)))
if(length(pageNo)!=1){
print(paste(x, 'page no cannot be determined'))
pageNo='?'
}
tag = paste0('SRD.',pageNo)
addTag(x,tagType = 'sources', tagToAdd = tag)
}) %>% invisible()
SRDspells %>% sapply(addTag,
tagType = 'sources',
tagToAdd = 'SRD') %>% invisible()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment