/scalate-core/src/main/scala/org/fusesource/scalate/console/EditLink.scala

http://github.com/scalate/scalate · Scala · 92 lines · 53 code · 18 blank · 21 comment · 13 complexity · 1792ffd0b9c63117199ad541c499b8af MD5 · raw file

  1. /**
  2. * Copyright (C) 2009-2011 the original author or authors.
  3. * See the notice.md file distributed with this work for additional
  4. * information regarding copyright ownership.
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. package org.fusesource.scalate.console
  19. import java.io.File
  20. import org.fusesource.scalate.RenderContext.captureNodeSeq
  21. import scala.xml.NodeSeq
  22. /**
  23. * @version $Revision : 1.1 $
  24. */
  25. object EditLink {
  26. var idePluginPort = 51235
  27. def editLink(file: String)(body: => Unit): NodeSeq = editLink(file, None, None)(body)
  28. def editLink(file: String, line: Option[Int], col: Option[Int])(body: => Unit): NodeSeq = {
  29. if (file == null) {
  30. Nil
  31. } else {
  32. System.getProperty("scalate.editor", "") match {
  33. case "textmate" => editLinkTextMate(file, line, col)(body)
  34. case "ide" => editLinkIdePlugin(file, line, col)(body)
  35. case "file" => editLinkFileScheme(file, line, col)(body)
  36. case _ =>
  37. if (isMacOsx && hasTextMate)
  38. editLinkTextMate(file, line, col)(body)
  39. else {
  40. editLinkFileScheme(file, line, col)(body)
  41. }
  42. }
  43. }
  44. }
  45. def editLinkFileScheme(file: String, line: Option[Int], col: Option[Int])(body: => Unit): NodeSeq = {
  46. val bodyText = captureNodeSeq(body)
  47. <a href={ "file://" + file } title="Open File" target="_blank">
  48. { bodyText }
  49. </a>
  50. }
  51. def editLinkTextMate(file: String, line: Option[Int], col: Option[Int])(body: => Unit): NodeSeq = {
  52. val bodyText = captureNodeSeq(body)
  53. val href = "txmt://open?url=file://" + file +
  54. (if (line.isDefined) "&line=" + line.get else "") +
  55. (if (col.isDefined) "&col=" + col.get else "")
  56. <a href={ href } title="Open in TextMate">
  57. { bodyText }
  58. </a>
  59. }
  60. def editLinkIdePlugin(file: String, line: Option[Int], col: Option[Int])(body: => Unit): NodeSeq = {
  61. val bodyText = captureNodeSeq(body)
  62. // The Atlassian IDE plugin seems to highlight the line after the actual line number, so lets subtract one
  63. val lineNumber = if (line.isDefined) {
  64. val n = line.get
  65. if (n > 1) n - 1 else 0
  66. } else 0
  67. <span>
  68. { bodyText }<img class="ide-icon tb_right_mid" id={ "ide-" + file.hashCode } title={ bodyText } onclick={ "this.src='http://localhost:" + idePluginPort + "/file?file=" + file + "&line=" + lineNumber + "&id=' + Math.floor(Math.random()*1000);" } alt="Open in IDE" src={ "http://localhost:" + idePluginPort + "/icon" }/>
  69. </span>
  70. }
  71. def isMacOsx = System.getProperty("os.name", "").contains("Mac OS X")
  72. def hasTextMate = exists("/Applications/TextMate.app") || exists("~/Applications/TextMate.app")
  73. def exists(fileName: String) = new File(fileName).exists
  74. }