You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

199 lines
11 KiB

  1. <?php
  2. // Project: Web Reference Database (refbase) <http://www.refbase.net>
  3. // Copyright: Matthias Steffens <mailto:refbase@extracts.de> and the file's
  4. // original author(s).
  5. //
  6. // This code is distributed in the hope that it will be useful,
  7. // but WITHOUT ANY WARRANTY. Please see the GNU General Public
  8. // License for more details.
  9. //
  10. // File: ./cite/formats/cite_latex.php
  11. // Repository: $HeadURL: file:///svn/p/refbase/code/branches/bleeding-edge/cite/formats/cite_latex.php $
  12. // Author(s): Matthias Steffens <mailto:refbase@extracts.de>
  13. //
  14. // Created: 10-Jun-06, 02:32
  15. // Modified: $Date: 2017-04-13 02:00:18 +0000 (Thu, 13 Apr 2017) $
  16. // $Author: karnesky $
  17. // $Revision: 1416 $
  18. // This is a citation format file (which must reside within the 'cite/formats/' sub-directory of your refbase root directory). It contains a
  19. // version of the 'citeRecords()' function that outputs a reference list from selected records in LaTeX format.
  20. //
  21. // --------------------------------------------------------------------
  22. // --- BEGIN CITATION FORMAT ---
  23. function citeRecords($result, $rowsFound, $query, $queryURL, $showQuery, $showLinks, $rowOffset, $showRows, $previousOffset, $nextOffset, $wrapResults, $citeStyle, $citeOrder, $citeType, $orderBy, $headerMsg, $userID, $viewType)
  24. {
  25. global $contentTypeCharset; // defined in 'ini.inc.php'
  26. global $client;
  27. // The array '$transtab_refbase_latex' contains search & replace patterns for conversion from refbase markup to LaTeX markup & entities.
  28. // Converts refbase fontshape markup (italic, bold) into LaTeX commands of the 'textcomp' package, super- and subscript as well as greek
  29. // symbols get converted into the respective commands in math mode. You may need to adopt the LaTeX markup to suit your individual needs.
  30. global $transtab_refbase_latex; // defined in 'transtab_refbase_latex.inc.php'
  31. // The arrays '$transtab_latin1_latex' and '$transtab_unicode_latex' provide translation tables for best-effort conversion of higher ASCII
  32. // characters from ISO-8859-1 (or Unicode, respectively) to LaTeX entities.
  33. global $transtab_latin1_latex; // defined in 'transtab_latin1_latex.inc.php'
  34. global $transtab_unicode_latex; // defined in 'transtab_unicode_latex.inc.php'
  35. // Initialize array variables:
  36. $yearsArray = array();
  37. $typeTitlesArray = array();
  38. // Define inline text markup to be used by the 'citeRecord()' function:
  39. $markupPatternsArray = array("bold-prefix" => "\\textbf{",
  40. "bold-suffix" => "}",
  41. "italic-prefix" => "\\textit{",
  42. "italic-suffix" => "}",
  43. "underline-prefix" => "\\ul{", // the '\ul' command requires '\usepackage{soul}'
  44. "underline-suffix" => "}",
  45. "endash" => "--", // or use '{\\textendash}'
  46. "emdash" => "---", // or use '{\\textemdash}'
  47. "ampersand" => "&", // conversion of ampersands is done below, after the citation has been generated
  48. "double-quote" => '"',
  49. "double-quote-left" => "{\\textquotedblleft}",
  50. "double-quote-right" => "{\\textquotedblright}",
  51. "single-quote" => "'", // same as for ampersands
  52. "single-quote-left" => "{\\textquoteleft}",
  53. "single-quote-right" => "{\\textquoteright}",
  54. "less-than" => "<",
  55. "greater-than" => ">",
  56. "newline" => "\n\n"
  57. );
  58. // Defines search & replace 'actions' that will be applied upon LaTeX output to all those refbase fields that are listed
  59. // in the corresponding 'fields' element:
  60. $latexSearchReplaceActionsArray = array(
  61. array('fields' => array("title", "publication", "abbrev_journal", "address", "keywords", "abstract", "orig_title", "series_title", "abbrev_series_title", "notes"),
  62. 'actions' => $transtab_refbase_latex
  63. )
  64. );
  65. // For CLI queries, we'll allow paging thru the result set, i.e. we honour the values of the CLI options '-S|--start' ('$rowOffset')
  66. // and '-R|--rows' ('$showRows') ('$rowOffset' and '$showRows' are re-assigned in function 'seekInMySQLResultsToOffset()' in 'include.inc.php')
  67. if (preg_match("/^cli/i", $client)) // if the query originated from a command line client such as the "refbase" CLI client ("cli-refbase-1.0")
  68. $showMaxRows = $showRows; // show only rows up to the value given in '$showRows'
  69. else
  70. $showMaxRows = $rowsFound; // otherwise show all rows
  71. // Setup the basic LaTeX document structure:
  72. $latexData = "%&LaTeX\n"
  73. . "\\documentclass{article}\n\n";
  74. // NOTE: the "Vancouver" & "Harvard 1" citation styles make use of the '\ul' command which requires '\usepackage{soul}'
  75. // TODO: figure out a better logic when to include the '\usepackage{soul}' statement (or should we simply always include it?)
  76. if (preg_match("/^(Vancouver|Harvard 1)$/i", $citeStyle))
  77. $latexData .= "\\usepackage{soul}\n";
  78. if ($contentTypeCharset == "UTF-8")
  79. $latexData .= "\\usepackage[utf8]{inputenc}\n";
  80. else
  81. $latexData .= "\\usepackage[latin1]{inputenc}\n";
  82. $latexData .= "\\usepackage[T1]{fontenc}\n"
  83. . "\\usepackage{textcomp}\n\n";
  84. $latexData .= "\\begin{document}\n\n";
  85. // Header:
  86. if (!empty($headerMsg))
  87. {
  88. // Remove any colon (":") from end of header message:
  89. $headerMsg = trimTextPattern($headerMsg, ":", false, true); // function 'trimTextPattern()' is defined in 'include.inc.php'
  90. // Decode any HTML entities:
  91. // (these may occur in the header message e.g. if the user's preferred display language is not English but German or French, etc)
  92. $headerMsg = decodeHTML($contentTypeCharset, $headerMsg); // function 'decodeHTML()' is defined in 'include.inc.php', and '$contentTypeCharset' is defined in 'ini.inc.php'
  93. // Convert refbase markup in the header message into appropriate LaTeX markup & entities:
  94. $headerMsg = searchReplaceText($transtab_refbase_latex, $headerMsg, true); // function 'searchReplaceText()' is defined in 'include.inc.php'
  95. // Attempt to convert higher ASCII chars (i.e., characters with an ASCII value of >= 128) in the header message to their corresponding LaTeX entities:
  96. if ($contentTypeCharset == "UTF-8")
  97. $headerMsg = searchReplaceText($transtab_unicode_latex, $headerMsg, false);
  98. else
  99. $headerMsg = searchReplaceText($transtab_latin1_latex, $headerMsg, false);
  100. $latexData .= "\\title{" . $headerMsg . "}\n\n"
  101. . "\\maketitle\n\n";
  102. }
  103. if (!preg_match("/type|year/i", $citeOrder))
  104. $latexData .= "\\begin{thebibliography}{" . $showMaxRows . "}\n\n";
  105. // LOOP OVER EACH RECORD:
  106. // Fetch one page of results (or less if on the last page)
  107. // (i.e., upto the limit specified in $showMaxRows) fetch a row into the $row array and ...
  108. for ($rowCounter=0; (($rowCounter < $showMaxRows) && ($row = @ mysqli_fetch_array($result))); $rowCounter++)
  109. {
  110. foreach ($row as $rowFieldName => $rowFieldValue)
  111. // Apply search & replace 'actions' to all fields that are listed in the 'fields' element of the arrays contained in '$latexSearchReplaceActionsArray':
  112. foreach ($latexSearchReplaceActionsArray as $fieldActionsArray)
  113. if (in_array($rowFieldName, $fieldActionsArray['fields']))
  114. $row[$rowFieldName] = searchReplaceText($fieldActionsArray['actions'], $row[$rowFieldName], true); // function 'searchReplaceText()' is defined in 'include.inc.php'
  115. // Order attributes according to the chosen output style & record type:
  116. $record = citeRecord($row, $citeStyle, $citeType, $markupPatternsArray, false); // function 'citeRecord()' is defined in the citation style file given in '$citeStyleFile' (which, in turn, must reside in the 'cite' directory of the refbase root directory), see function 'generateCitations()'
  117. // Print out the current record:
  118. if (!empty($record)) // unless the record buffer is empty...
  119. {
  120. // Print any section heading(s):
  121. if (preg_match("/year|type/i", $citeOrder))
  122. {
  123. list($yearsArray, $typeTitlesArray, $sectionHeading) = generateSectionHeading($yearsArray, $typeTitlesArray, $row, $citeOrder, "", "", "\\section*{", "}\n\n", "\\subsection*{", "}\n\n"); // function 'generateSectionHeading()' is defined in 'cite.inc.php'
  124. $latexData .= $sectionHeading;
  125. }
  126. // Attempt to convert higher ASCII chars (i.e., characters with an ASCII value of >= 128) to their corresponding LaTeX entities:
  127. if ($contentTypeCharset == "UTF-8")
  128. $recordEncoded = searchReplaceText($transtab_unicode_latex, $record, false); // function 'searchReplaceText()' is defined in 'include.inc.php'
  129. else
  130. $recordEncoded = searchReplaceText($transtab_latin1_latex, $record, false);
  131. // Write LaTeX paragraph:
  132. if (!preg_match("/type|year/i", $citeOrder))
  133. {
  134. // This is a stupid hack that maps the names of the '$row' array keys to those used
  135. // by the '$formVars' array (which is required by function 'generateCiteKey()')
  136. // (eventually, the '$formVars' array should use the MySQL field names as names for its array keys)
  137. $formVars = buildFormVarsArray($row); // function 'buildFormVarsArray()' is defined in 'include.inc.php'
  138. // Generate or extract the cite key for this record:
  139. // NOTE: currently, the following placeholders are not available for citation output:
  140. // <:keywords:>, <:issn:>, <:area:>, <:notes:>, <:userKeys:>
  141. // if the cite key specification uses one of these placeholders, it will get ignored
  142. $citeKey = generateCiteKey($formVars); // function 'generateCiteKey()' is defined in 'include.inc.php'
  143. if (!empty($citeKey))
  144. // Use the custom cite key that's been build according to the user's individual export options:
  145. $latexData .= "\\bibitem{" . $citeKey . "} ";
  146. else
  147. // The '\bibitem' command requires a cite key, which is why we'll include the record's serial number
  148. // even when the user's export options specify 'export_cite_keys=no' or 'autogenerate_cite_keys=no':
  149. $latexData .= "\\bibitem{" . $row['serial'] . "} ";
  150. }
  151. $latexData .= $recordEncoded . "\n\n"; // create paragraph with encoded record text
  152. }
  153. }
  154. if (!preg_match("/type|year/i", $citeOrder))
  155. $latexData .= "\\end{thebibliography}\n\n";
  156. $latexData .= "\\end{document}\n\n";
  157. return $latexData;
  158. }
  159. // --- END CITATION FORMAT ---
  160. ?>