wdConvertHtml.R 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. # * Author: Bangyou Zheng ([email protected])
  2. # * Created: 11:29 AM Thursday, 20 March 2014
  3. # * Copyright: AS IS
  4. # *
  5. # Convert wheat documentation to html
  6. rm(list = ls())
  7. doc <- readLines('WheatDocumentation.lyx')
  8. pos <- grep('dev = \'pdf\'', doc)
  9. doc[pos] <- gsub('dev = \'pdf\'', 'dev = \'png\'', doc[pos])
  10. pos <- grep('\\.pdf', doc)
  11. doc[pos] <- gsub('\\.pdf', '', doc[pos])
  12. pos <- grep('source\\(\'Rcode/wdVisXY.R\'\\)', doc)
  13. doc[pos] <- gsub('source\\(\'Rcode/wdVisXY.R\'\\)',
  14. 'source\\(\'../Rcode/wdVisXY.R\'\\)', doc[pos])
  15. pos <- grep('source\\(\'Rcode/wdFunctions.R\'\\)', doc)
  16. doc[pos] <- gsub('source\\(\'Rcode/wdFunctions.R\'\\)',
  17. 'source\\(\'../Rcode/wdFunctions.R\'\\)', doc[pos])
  18. pos <- grep('xmlInternalTreeParse\\(\'wheat.xml\'\\)', doc)
  19. doc[pos] <- gsub('xmlInternalTreeParse\\(\'wheat.xml\'\\)',
  20. 'xmlInternalTreeParse\\(\'../wheat.xml\'\\)', doc[pos])
  21. figures <- c('wdBiomassPartitioning.png', 'wdWheatPhenology.png',
  22. 'wdBiomassPartition.png')
  23. dir.create('html')
  24. setwd('html')
  25. dir.create('figure')
  26. file.copy(paste0('../figure/', figures), 'figure/')
  27. file.copy('../tex4ht.cfg', '.')
  28. writeLines(doc, 'wheat.lyx')
  29. system('lyx -f --export "latex" wheat.lyx')
  30. # Change the path of bibliography
  31. tex <- readLines('wheat.tex')
  32. pos <- grep('\\\\bibliographystyle', tex)
  33. tex[pos] <- gsub('(\\\\bibliographystyle\\{).*(\\})',
  34. '\\1../elsart-harv\\2', tex[pos])
  35. pos <- grep('\\\\bibliography\\{', tex)
  36. tex[pos] <- gsub('(.*\\\\bibliography\\{).*(\\})',
  37. '\\1../wd\\2', tex[pos])
  38. pos <- grep('type=eps', tex)
  39. tex[pos] <- gsub(', type=eps', '', tex[pos])
  40. pos <- grep('.*\\\\includegraphics.*\\{.*\\}.*', tex)
  41. tex[pos] <- gsub('(.*\\\\includegraphics.*\\{)(.*/.*)(\\}.*)',
  42. '\\1\\2.png\\3', tex[pos])
  43. pos <- grep('wdBiomassPartitioning.png', tex)
  44. tex[pos] <- gsub('(.*\\\\includegraphics.*\\{)(.*)(\\}.*)',
  45. '\\1\\figure/wdBiomassPartitioning.png\\3', tex[pos])
  46. writeLines(tex, 'wheat.tex')
  47. # Convert to html
  48. system('latex wheat')
  49. system('bibtex wheat')
  50. system('latex wheat')
  51. system('latex wheat')
  52. system('htlatex wheat "tex4ht,html,word"')
  53. # Change the png path
  54. pngs <- list.files('.', 'wheat\\d+x.png')
  55. dir.create('equation')
  56. file.copy(pngs, 'equation')
  57. file.remove(pngs)
  58. html <- readLines('wheat.html')
  59. pos <- grep('(^.*src=")(wheat\\d+x\\.png)(".*$)', html)
  60. html[pos] <- gsub('(^.*src=")(wheat\\d+x\\.png)(".*$)',
  61. '\\1equation/\\2\\3', html[pos])
  62. pos <- grep('^<img', html)
  63. html[pos] <- paste0('<div align="middle">', html[pos])
  64. for (i in seq(along = pos))
  65. {
  66. pos_i <- grep('>', html[seq(pos[i] + 1, pos[i] + 10)])[1] + pos[i]
  67. pos_2 <- as.numeric(regexpr('>', html[pos_i]))
  68. html[pos_i] <- paste0(substring(html[pos_i], 1, pos_2),
  69. '</div>',
  70. substring(html[pos_i], pos_2 + 1, nchar(html[pos_i])))
  71. }
  72. writeLines(html, 'wheat.html')
  73. files <- list.files('.', '\\.', full.names = TRUE)
  74. file.remove(files[!(files %in%
  75. list.files('.', 'html|css', full.names = TRUE))])
  76. setwd('..')