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.

147 lines
7.5 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_markdown.php
  11. // Repository: $HeadURL: file:///svn/p/refbase/code/branches/bleeding-edge/cite/formats/cite_markdown.php $
  12. // Author(s): Matthias Steffens <mailto:refbase@extracts.de>
  13. //
  14. // Created: 10-Jun-06, 02:58
  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 Markdown format. Markdown is a plain text
  20. // formatting syntax as well as a software tool that converts the plain text formatting back to HTML (<http://daringfireball.net/projects/markdown/>)
  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_markdown' contains search & replace patterns for conversion from refbase markup to Markdown markup & entities
  28. global $transtab_refbase_markdown; // defined in 'transtab_refbase_markdown.inc.php'
  29. $markdownData = ""; // make sure that our buffer variable is empty
  30. // Header:
  31. if (!empty($headerMsg))
  32. {
  33. // Decode any HTML entities:
  34. // (these may occur in the header message e.g. if the user's preferred display language is not English but German or French, etc)
  35. $headerMsg = decodeHTML($contentTypeCharset, $headerMsg); // function 'decodeHTML()' is defined in 'include.inc.php', and '$contentTypeCharset' is defined in 'ini.inc.php'
  36. // Convert refbase markup in the header message into appropriate Markdown markup & entities:
  37. $headerMsg = searchReplaceText($transtab_refbase_markdown, $headerMsg, true); // function 'searchReplaceText()' is defined in 'include.inc.php'
  38. $markdownData .= "# $headerMsg #\n\n";
  39. }
  40. // Initialize array variables:
  41. $yearsArray = array();
  42. $typeTitlesArray = array();
  43. // Define inline text markup to be used by the 'citeRecord()' function:
  44. $markupPatternsArray = array("bold-prefix" => "**",
  45. "bold-suffix" => "**",
  46. "italic-prefix" => "_",
  47. "italic-suffix" => "_",
  48. "underline-prefix" => "<u>",
  49. "underline-suffix" => "</u>",
  50. "endash" => "&ndash;",
  51. "emdash" => "&mdash;",
  52. "ampersand" => "&",
  53. "double-quote" => '"',
  54. "double-quote-left" => "&ldquo;",
  55. "double-quote-right" => "&rdquo;",
  56. "single-quote" => "'",
  57. "single-quote-left" => "&lsquo;",
  58. "single-quote-right" => "&rsquo;",
  59. "less-than" => "<",
  60. "greater-than" => ">",
  61. "newline" => " \n"
  62. );
  63. // Defines search & replace 'actions' that will be applied upon Markdown output to all those refbase fields that are listed
  64. // in the corresponding 'fields' element:
  65. $markdownSearchReplaceActionsArray = array(
  66. array('fields' => array("title", "publication", "abbrev_journal", "address", "keywords", "abstract", "orig_title", "series_title", "abbrev_series_title", "notes"),
  67. 'actions' => $transtab_refbase_markdown
  68. )
  69. );
  70. // For CLI queries, we'll allow paging thru the result set, i.e. we honour the values of the CLI options '-S|--start' ('$rowOffset')
  71. // and '-R|--rows' ('$showRows') ('$rowOffset' and '$showRows' are re-assigned in function 'seekInMySQLResultsToOffset()' in 'include.inc.php')
  72. 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")
  73. $showMaxRows = $showRows; // show only rows up to the value given in '$showRows'
  74. else
  75. $showMaxRows = $rowsFound; // otherwise show all rows
  76. // LOOP OVER EACH RECORD:
  77. // Fetch one page of results (or less if on the last page)
  78. // (i.e., upto the limit specified in $showMaxRows) fetch a row into the $row array and ...
  79. for ($rowCounter=0; (($rowCounter < $showMaxRows) && ($row = @ mysqli_fetch_array($result))); $rowCounter++)
  80. {
  81. foreach ($row as $rowFieldName => $rowFieldValue)
  82. // Apply search & replace 'actions' to all fields that are listed in the 'fields' element of the arrays contained in '$markdownSearchReplaceActionsArray':
  83. foreach ($markdownSearchReplaceActionsArray as $fieldActionsArray)
  84. if (in_array($rowFieldName, $fieldActionsArray['fields']))
  85. $row[$rowFieldName] = searchReplaceText($fieldActionsArray['actions'], $row[$rowFieldName], true); // function 'searchReplaceText()' is defined in 'include.inc.php'
  86. // Order attributes according to the chosen output style & record type:
  87. $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()'
  88. // Print out the current record:
  89. if (!empty($record)) // unless the record buffer is empty...
  90. {
  91. // Print any section heading(s):
  92. if (preg_match("/year|type/i", $citeOrder))
  93. {
  94. $headingPrefix = "";
  95. $headingSuffix = "";
  96. if (!empty($headerMsg)) // if there's a custom header message available, we decrease the heading level of sections & subsections by one (since the header message has level 1)
  97. {
  98. $sectionMarkupPrefix = "## ";
  99. $sectionMarkupSuffix = " ##\n\n";
  100. $subSectionMarkupPrefix = "### ";
  101. $subSectionMarkupSuffix = " ###\n\n";
  102. }
  103. else // no custom header message given
  104. {
  105. $sectionMarkupPrefix = "# ";
  106. $sectionMarkupSuffix = " #\n\n";
  107. $subSectionMarkupPrefix = "## ";
  108. $subSectionMarkupSuffix = " ##\n\n";
  109. }
  110. list($yearsArray, $typeTitlesArray, $sectionHeading) = generateSectionHeading($yearsArray, $typeTitlesArray, $row, $citeOrder, $headingPrefix, $headingSuffix, $sectionMarkupPrefix, $sectionMarkupSuffix, $subSectionMarkupPrefix, $subSectionMarkupSuffix); // function 'generateSectionHeading()' is defined in 'cite.inc.php'
  111. $markdownData .= $sectionHeading;
  112. }
  113. // Write plain TEXT paragraph:
  114. $markdownData .= $record . "\n\n"; // create paragraph with encoded record text
  115. }
  116. }
  117. return $markdownData;
  118. }
  119. // --- END CITATION FORMAT ---
  120. ?>