KXStudio Website https://kx.studio/
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.

133 lines
5.7KB

  1. <?php
  2. class Chat extends Feathers implements Feather {
  3. public function __init() {
  4. $this->setField(array("attr" => "title",
  5. "type" => "text",
  6. "label" => __("Title", "chat"),
  7. "optional" => true));
  8. $this->setField(array("attr" => "dialogue",
  9. "type" => "text_block",
  10. "label" => __("Dialogue", "chat"),
  11. "preview" => true,
  12. "help" => "chat_dialogue",
  13. "bookmarklet" => "selection"));
  14. $this->customFilter("dialogue", "format_dialogue");
  15. $this->setFilter("title", array("markup_title", "markup_post_title"));
  16. $this->setFilter("dialogue", array("markup_text", "markup_post_text"));
  17. $this->respondTo("preview_chat", "format_dialogue");
  18. $this->respondTo("help_chat_dialogue", "help");
  19. }
  20. public function submit() {
  21. if (empty($_POST['dialogue']))
  22. error(__("Error"), __("Dialogue can't be blank."));
  23. fallback($_POST['slug'], sanitize($_POST['title']));
  24. return Post::add(array("title" => $_POST['title'],
  25. "dialogue" => $_POST['dialogue']),
  26. $_POST['slug'],
  27. Post::check_url($_POST['slug']));
  28. }
  29. public function update($post) {
  30. if (empty($_POST['dialogue']))
  31. error(__("Error"), __("Dialogue can't be blank."));
  32. $post->update(array("title" => $_POST['title'],
  33. "dialogue" => $_POST['dialogue']));
  34. }
  35. public function title($post) {
  36. $dialogue = oneof($post->dialogue_unformatted, $post->dialogue);
  37. $dialogue = explode("\n", $dialogue);
  38. $line = preg_replace("/^\s*[\[\(]?[0-9]{1,2}:[0-9]{2}(:[0-9]{2})?\s*(pm|am)?[\]|\)]?\s*/i", "", $dialogue[0]);
  39. $first_line = preg_replace("/([<]?)([^:|>]+)( \(me\)?)(:|>) (.+)/i", "\\1\\2\\4 \\5", $dialogue[0]);
  40. return oneof($post->title, $first_line);
  41. }
  42. public function excerpt($post) {
  43. return $post->dialogue;
  44. }
  45. public function feed_content($post) {
  46. return $post->dialogue;
  47. }
  48. public function format_dialogue($text, $post = null) {
  49. if (isset($post))
  50. $post->dialogue_unformatted = $text;
  51. $split = explode("\n", $text);
  52. $return = '<ul class="dialogue">';
  53. $count = 0;
  54. $my_name = "";
  55. $links = array();
  56. foreach ($split as $line) {
  57. # Remove the timstamps
  58. $line = preg_replace("/^\s*[\[\(]?[0-9]{1,2}:[0-9]{2}(:[0-9]{2})?\s*(pm|am)?[\]|\)]?\s*/i", "", $line);
  59. preg_match("/(<?)(.+)(:|>)\s*(.+)/i", $line, $matches);
  60. if (empty($matches))
  61. continue;
  62. if (preg_match("/\s*\(([^\)]+)\)$/", $matches[2], $attribution))
  63. if ($attribution[1] == "me") {
  64. $my_name = $matches[2] = str_replace($attribution[0], "", $matches[2]);
  65. } else {
  66. $matches[2] = str_replace($attribution[0], "", $matches[2]);
  67. $links[$matches[2]] = $attribution[1];
  68. }
  69. $link = oneof(@$links[$matches[2]], "");
  70. $me = ($my_name == $matches[2] ? " me" : "");
  71. $username = $matches[1].$matches[2].$matches[3];
  72. $class = ($count % 2 ? "even" : "odd");
  73. $return.= '<li class="'.$class.$me.'">';
  74. if (!empty($link))
  75. $return.= '<span class="label">'.$matches[1].'<a href="'.$link.'">'.fix($matches[2], false).'</a>'.$matches[3].'</span> '.$matches[4]."\n";
  76. else
  77. $return.= '<span class="label">'.fix($username, false).'</span> '.$matches[4]."\n";
  78. $return.= '</li>';
  79. $count++;
  80. }
  81. $return.= "</ul>";
  82. # If they're previewing.
  83. if (!isset($post))
  84. $return = preg_replace("/(<li class=\"(even|odd) me\"><span class=\"label\">)(.+)(<\/span> (.+)\n<\/li>)/", "\\1<strong>\\3</strong>\\4", $return);
  85. return $return;
  86. }
  87. public function help() {
  88. $title = __("Dialogue Formatting", "chat");
  89. $body = "<p>".__("To give yourself a special CSS class, append \" (me)\" to your username, like so:", "chat")."</p>\n";
  90. $body.= "<ul class=\"list\">\n";
  91. $body.= "\t<li>&quot;&lt;Alex&gt;&quot; &rarr; &quot;&lt;Alex (me)&gt;&quot;</li>\n";
  92. $body.= "\t<li>&quot;Alex:&quot; &rarr; &quot;Alex (me):&quot;</li>\n";
  93. $body.= "</ul>\n";
  94. $body.= "<p>".__("This only has to be done to the first occurrence of the username.", "chat")."</p>";
  95. $body.= "<p>".__("To attribute a name to a URL, append the URL in parentheses, preceded by a space, to the username:", "chat")."</p>\n";
  96. $body.= "<ul class=\"list\">\n";
  97. $body.= "\t<li>&quot;&lt;John&gt;&quot; &rarr; &quot;&lt;John (http://example.com/)&gt;&quot;</li>\n";
  98. $body.= "\t<li>&quot;John:&quot; &rarr; &quot;John (http://example.com/):&quot;</li>\n";
  99. $body.= "</ul>\n";
  100. $body.= "<p>".__("This also only has to be done to the first occurrence of the username. It cannot be combined with attributing someone as yourself (because they're already at your site anyway).", "chat")."</p>";
  101. return array($title, $body);
  102. }
  103. }