Documentation.st 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  1. Smalltalk current createPackage: 'Documentation' properties: #{}!
  2. Object subclass: #DocumentationBuilder
  3. instanceVariableNames: 'chapters announcer widget'
  4. category: 'Documentation'!
  5. !DocumentationBuilder methodsFor: 'accessing'!
  6. chapters
  7. ^chapters ifNil: [chapters := self buildChapters]
  8. !
  9. announcer
  10. ^announcer ifNil: [announcer := Announcer new]
  11. !
  12. widget
  13. ^widget ifNil: [widget := DocumentationWidget on: self]
  14. ! !
  15. !DocumentationBuilder methodsFor: 'building'!
  16. buildChapters
  17. ^((self class methodDictionary values sorted: [:a :b | a selector < b selector])
  18. select: [:each | each category = 'chapters'])
  19. collect: [:each | self perform: each selector]
  20. !
  21. buildOn: aCanvas
  22. aCanvas with: self widget
  23. !
  24. buildOnJQuery: aJQuery
  25. self buildOn: (HTMLCanvas onJQuery: aJQuery)
  26. ! !
  27. !DocumentationBuilder methodsFor: 'chapters'!
  28. ch1introduction
  29. ^DocChapter new
  30. title: 'Introduction';
  31. contents: '
  32. ##Amber Smalltalk in a nutshell.
  33. Amber is an implementation of the Smalltalk-80 language.
  34. It allows developers to write client-side heavy web applications in Smalltalk. Amber includes an integrated development environment with a class browser, workspace and transcript.
  35. Amber includes the following features:
  36. - It is semantically and syntactically equivalent to Pharo Smalltalk (the implementation considered as the reference)
  37. - It is written in itself and compiles into efficient JavaScript
  38. - A canvas API similar to Seaside to generate HTML
  39. - A jQuery binding.
  40. ## Disclaimer
  41. This documentation doesn''t aim to teach Smalltalk.
  42. Knowledge of Smalltalk is needed to understand the topics covered in this documentation.
  43. If you want to learn the Smalltalk language, you can read the excellent [Pharo By Example](http://www.pharobyexample.org) book.
  44. '
  45. !
  46. ch2differencesWithOtherSmalltalks
  47. ^DocChapter new
  48. title: 'Differences with other Smalltalks';
  49. contents: '
  50. Amber has some differences with other Smalltalk implementations.
  51. Because it maps Smalltalk constructs one-to-one with the JavaScript equivalent, including Smalltalk classes to JavaScript constructors, the core class library is simplified compared to Pharo Smalltalk.
  52. The following list explains the main differences:
  53. - The collection class hierarchy is simpler compared to most Smalltalk implementations. As of today, there is no SortedCollection. The size of arrays is dynamic, and they behave like an ordered collection.
  54. They can also be sorted with the #sort* methods.
  55. - The Date class behaves like the Date and TimeStamp classes in Pharo Smalltalk. Therefore both Date today and Date now are valid in Amber.
  56. '
  57. !
  58. ch3GettingStarted
  59. ^DocChapter new
  60. title: 'Getting started';
  61. contents: '
  62. To get started hacking in Amber you can basically take three routes, independent of your platform:
  63. 1. Just try it out at http://www.amber-lang.net (click the "Class browser" button) - but you will **not be able to save any code you write**!!
  64. Still, it works fine for looking at the IDE and playing around. Just don''t press F5/reload - it will bring you back to zero.
  65. (Well, if you still want to develop and save code online someone has set up this site seems for free use: https://www.screwtopdb.com/amberstore/topics?name=amberstore/amber.html )
  66. 2. Download an Amber zip-ball, install Nodejs, fire up the Amber server and then open Amber from localhost - then you **can save code**. Detailed instructions are below!!
  67. 3. Same as above but install git first and get a proper clone from http://github.com/NicolasPetton/amber instead of a zip/tar-ball.
  68. If you want to **contribute to Amber itself** this is really what you want to do. It requires installing git first, but it is quite simple - although we leave this bit as an "exercise to the reader" :)
  69. **PLEASE NOTE:** Amber core developers use Linux.
  70. We do not want to introduce dependencies that aren''t cross platform - but currently amberc (the command line compiler) is a bash script and we also use Makefiles
  71. (for building Amber itself and server side examples) written on Linux/Unix. So using Windows is currently a bit limited - you can''t run "make" in the .st directory to rebuild whole of Amber for example.
  72. BUT... if you only want to use Amber to build web client apps and not really get involved in hacking Amber itself - then you should be fine!!
  73. ## Downloading Amber
  74. Currently you can download in zip or tar-ball format, either cutting edge or a release. [Downloads are available here](https://github.com/NicolasPetton/amber/archives/amber).
  75. At the moment of writing you have release [0.9 as zip](https://github.com/NicolasPetton/amber/zipball/0.9) or [tar](https://github.com/NicolasPetton/amber/tarball/0.9),
  76. or [cutting edge as zip](https://github.com/NicolasPetton/amber/zipball/amber) or [tar](https://github.com/NicolasPetton/amber/tarball/amber).
  77. At the moment this is just a **1.5Mb download**, so its very small. Unpack wherever you like, but I would rename the directory that is unpacked to something slightly shorter - like say "amber-0.9" or just "amber".
  78. And yes, at this point you can double click the index.html file in the amber directory to get the IDE up, but again, **you will not be able to save code**. So please continue below :)
  79. ## Installing Node.js
  80. [Node](http://www.nodejs.org) (for short) is simply the V8 Javascript VM from Google (used in Chrome) hooked together with some hard core C-libraries for doing "evented I/O".
  81. Basically it''s Javascript for the server - on asynch steroids. Amber runs fine in Node and we use it for several Amber tools, like amberc (the command line Amber compiler) or the Amber server (see below).
  82. There are also several Amber-Node example to look at if you want to play with running Amber programs server side. **In short - you really want to install Nodejs. :)**
  83. - Installing Node on Linux can be done using your package tool of choice ("apt-get install nodejs" for example) or any other way described at [the download page](http://nodejs.org/#download).
  84. - Installing Node on MacOS seems to be easiest by getting it from [here](https://sites.google.com/site/nodejsmacosx/).
  85. - Installing Node on Windows is probably done best by using the [download from Nodejs.org](http://nodejs.org/#download). This is not an installer, it is instead simply the node executable - **node.exe**.
  86. - Put node.exe somewhere in your path. In Windows7 I can run a command prompt "As administrator" (right click the command prompt in Start menu) and then just "copy node.exe c:\windows\" or such.
  87. ## Starting Amber server
  88. Nicolas has written a minimal webDAV server that is the easiest way to get up and running Amber with the ability to save code. This little server is written in... Amber!!
  89. And it runs on top of Node. So to start it up serving your brand new directory tree of sweet Amber you do:
  90. cd amber (or whatever you called the directory you unpackaged)
  91. ./bin/server (in windows you type "node server\server.js" instead)
  92. It should say it is listening on port 4000. If it does, hooray!! That means both Node and Amber are good. In Windows you might get a question about opening that port in the local firewall - yep, do it!!
  93. ## Firing up Amber
  94. The Amber IDE is written in... Amber. It uses JQuery and runs right in your browser as a ... well, a web page.
  95. We could open it up just using a file url - but the reason we performed the previous steps is so that we can load the IDE web page from a server that can handle PUTs (webDAV) of source code.
  96. According to web security Amber can only do PUT back to the same server it was loaded from. Thus we instead want to open it through our little server now listening on port 4000:
  97. http://localhost:4000/index.html
  98. Clicking the above link should get your Amber running.
  99. To verify that you can indeed commit - just select a Package in the browser, like say "Examples" and press "Commit package" button. **If all goes well nothing happens :)**.
  100. So in order to really know if it worked we can check the modified date on the files **amber/st/Examples.st**, **amber/js/Examples.js** and **amber/js/Examples.deploy.js** - they should be brand new.
  101. NOTE: We can use any webDAV server and Apache2 has been used earlier and works fine. But the Amber server is smaller and simpler to start.
  102. '
  103. !
  104. ch4FirstApp
  105. ^DocChapter new
  106. title: 'A first application';
  107. contents: '
  108. Let''s make Hello World in Amber.
  109. First, you need a place for your new project. I made a new directory under amber:
  110. amber/projects/hello
  111. This will store your project files. To get started, add a new index.html file to this folder, as well as empty js and st folders.
  112. Your index.html can be really basic. The most important thing it does is include amber.js and run loadAmber. Here is a basic index.html you can use:
  113. <!!DOCTYPE html>
  114. <html>
  115. <head>
  116. <title>My First Amber Project</title>
  117. <script src="../../js/amber.js" type="text/javascript"></script>
  118. <script type="text/javascript">
  119. loadAmber({
  120. files: [],
  121. prefix: ''projects/hello/js'',
  122. ready: function() {
  123. }});
  124. </script>
  125. </head>
  126. <body>
  127. <article>
  128. <h1>My First Amber Project</h1>
  129. <button onclick="smalltalk.Browser._open()">class browser</button>
  130. <button id="sayHello">say hello</button>
  131. </article>
  132. </body>
  133. </html>
  134. Now start up amber with node.js and navigate to http://localhost:4000/projects/hello/index.html
  135. It''s boring so far, so lets write some code. Click the button to open the class browser. Find an existing class and change its name to Hello and its package to HelloApp.
  136. Then click save. This creates a new class and leaves the old one intact, it doesn''t overwrite it. Your class will look like this:
  137. Object subclass: #Hello
  138. instanceVariableNames: ''''
  139. package: ''HelloApp''
  140. Now click save and navigate to your new class in its new package.
  141. Then click ''commit package''. You just created a new class and saved your work.
  142. On your file system check out your js and st folders. Your new class is now saved in both JavaScript and Smalltalk.
  143. Now, refresh your browser page and reopen the class browser. Oh no, your new class is gone!! To load your new class automatically, you have to add it in index.html. Make your JavaScript look like this:
  144. loadAmber({
  145. files: [''HelloApp.js''],
  146. prefix: ''projects/hello/js'',
  147. ready: function() {
  148. }});
  149. Save and refresh again. Now your class is loaded and shows up in the class browser.
  150. Now, let''s make this class do something. Create a new message in the class browser by navigating to your class, then clicking ''not yet classified'' and fill in a simple message. Try this for example:
  151. begin
  152. "Makes me say hello to the user."
  153. | msg button |
  154. msg := ''Hello world!!''.
  155. button := ''#sayHello'' asJQuery.
  156. button click: [button after: ''<p>'' , msg , ''</p>''].
  157. Your message isn''t too helpful if it doesn''t get called. Save it, commit the package, then edit index.html again. You can write JavaScript code that sends a message to Smalltalk:
  158. loadAmber({
  159. files: [''HelloApp.js''],
  160. prefix: ''projects/hello/js'', // path for js files i think
  161. ready: function() {
  162. $(function() {
  163. smalltalk.Hello._new()._begin();
  164. });
  165. }});
  166. From there, you can create new Smalltalk classes and messages to build up your app. Enjoy!!
  167. '
  168. !
  169. ch5Index
  170. ^ClassesIndexChapter new
  171. !
  172. ch6KernelObjects
  173. ^PackageDocChapter new
  174. title: 'Kernel-Objects';
  175. package: (Package named: 'Kernel-Objects');
  176. contents: ''
  177. ! !
  178. DocumentationBuilder class instanceVariableNames: 'current'!
  179. !DocumentationBuilder class methodsFor: 'accessing'!
  180. current
  181. ^current ifNil: [current := self new]
  182. ! !
  183. !DocumentationBuilder class methodsFor: 'initialization'!
  184. initialize
  185. self current buildOnJQuery: (window jQuery: 'body')
  186. ! !
  187. Widget subclass: #DocChapter
  188. instanceVariableNames: 'title contents parent'
  189. category: 'Documentation'!
  190. !DocChapter methodsFor: 'accessing'!
  191. title
  192. ^title ifNil: ['']
  193. !
  194. title: aString
  195. title := aString
  196. !
  197. contents
  198. ^contents
  199. !
  200. contents: aString
  201. contents := aString
  202. !
  203. htmlContents
  204. ^(Showdown at: #converter) new makeHtml: self contents
  205. !
  206. chapters
  207. "A doc chapter can contain sub chapters"
  208. ^#()
  209. !
  210. cssClass
  211. ^'doc_chapter'
  212. !
  213. level
  214. ^self parent ifNil: [1] ifNotNil: [self parent level +1]
  215. !
  216. level: anInteger
  217. level := anInteger
  218. !
  219. parent
  220. ^parent
  221. !
  222. parent: aChapter
  223. parent := aChapter
  224. ! !
  225. !DocChapter methodsFor: 'actions'!
  226. selectClass: aClass
  227. DocumentationBuilder current announcer announce: (ClassSelectionAnnouncement on: aClass)
  228. !
  229. selectChapter: aChapter
  230. DocumentationBuilder current widget selectChapter: aChapter
  231. ! !
  232. !DocChapter methodsFor: 'rendering'!
  233. renderOn: html
  234. html div
  235. class: self cssClass;
  236. with: [
  237. self renderDocOn: html.
  238. self renderLinksOn: html]
  239. !
  240. renderDocOn: html
  241. | div |
  242. html h1 with: self title.
  243. self renderNavigationOn: html.
  244. div := html div class: 'contents'.
  245. div asJQuery html: self htmlContents
  246. !
  247. renderNavigationOn: html
  248. self parent ifNotNil: [
  249. html div
  250. class: 'navigation'; with: [
  251. html a
  252. with: '← back to ', self parent title;
  253. onClick: [self selectChapter: self parent]]]
  254. !
  255. renderLinksOn: html
  256. html ul
  257. class: 'links';
  258. with: [
  259. self chapters do: [:each |
  260. html li with: [
  261. html a
  262. with: each title;
  263. onClick: [self selectChapter: each]]]]
  264. ! !
  265. DocChapter subclass: #PackageDocChapter
  266. instanceVariableNames: 'package chapters'
  267. category: 'Documentation'!
  268. !PackageDocChapter methodsFor: 'accessing'!
  269. package
  270. ^package
  271. !
  272. package: aPackage
  273. package := aPackage.
  274. chapters := (aPackage classes sorted: [:a :b | a name < b name]) collect: [:each |
  275. (ClassDocChapter on: each)
  276. parent: self;
  277. yourself]
  278. !
  279. title
  280. ^'Package ', self package name
  281. !
  282. chapters
  283. ^chapters
  284. !
  285. contents
  286. ^'Classes in package ', self package name, ':'
  287. ! !
  288. !PackageDocChapter class methodsFor: 'instance creation'!
  289. on: aPackage
  290. ^self new
  291. package: aPackage;
  292. yourself
  293. ! !
  294. DocChapter subclass: #ClassDocChapter
  295. instanceVariableNames: 'theClass'
  296. category: 'Documentation'!
  297. !ClassDocChapter methodsFor: 'accessing'!
  298. theClass
  299. ^theClass
  300. !
  301. theClass: aClass
  302. theClass := aClass.
  303. self subscribe
  304. !
  305. contents
  306. ^self theClass comment isEmpty
  307. ifTrue: [self theClass name, ' is not documented yet.']
  308. ifFalse: [self theClass comment]
  309. !
  310. cssClass
  311. ^'doc_class ', super cssClass
  312. !
  313. title
  314. ^self theClass name
  315. !
  316. subscribe
  317. DocumentationBuilder current announcer
  318. on: ClassSelectionAnnouncement do: [:ann |
  319. ann theClass = self theClass ifTrue: [
  320. DocumentationBuilder current widget selectChapter: self]]
  321. ! !
  322. !ClassDocChapter methodsFor: 'rendering'!
  323. renderLinksOn: html
  324. html ul
  325. class: 'links';
  326. with: [
  327. html li with: [html a
  328. with: 'Browse this class';
  329. onClick: [Browser openOn: self theClass]]]
  330. ! !
  331. !ClassDocChapter class methodsFor: 'accessing'!
  332. on: aClass
  333. ^self new
  334. theClass: aClass;
  335. yourself
  336. ! !
  337. Widget subclass: #DocumentationWidget
  338. instanceVariableNames: 'builder selectedChapter chapterDiv'
  339. category: 'Documentation'!
  340. !DocumentationWidget methodsFor: 'accessing'!
  341. builder
  342. ^builder
  343. !
  344. builder: aDocumentationBuilder
  345. builder := aDocumentationBuilder
  346. !
  347. chapters
  348. ^self builder chapters
  349. !
  350. selectedChapter
  351. ^selectedChapter ifNil: [selectedChapter := self chapters first]
  352. !
  353. selectedChapter: aChapter
  354. ^selectedChapter := aChapter
  355. ! !
  356. !DocumentationWidget methodsFor: 'actions'!
  357. selectChapter: aChapter
  358. self selectedChapter: aChapter.
  359. self updateChapterDiv
  360. ! !
  361. !DocumentationWidget methodsFor: 'rendering'!
  362. renderOn: html
  363. html div
  364. class: 'documentation';
  365. with: [
  366. self renderMenuOn: html.
  367. chapterDiv := html div.
  368. self updateChapterDiv]
  369. !
  370. renderMenuOn: html
  371. html div
  372. class: 'menu';
  373. with: [
  374. html ol with: [
  375. self chapters do: [:each |
  376. html li with: [
  377. self renderChapterMenu: each on: html]]]]
  378. !
  379. renderChapterMenu: aChapter on: html
  380. html a
  381. with: aChapter title;
  382. onClick: [
  383. self selectChapter: aChapter].
  384. html ol with: [
  385. aChapter chapters do: [:each |
  386. html li with: [
  387. self renderChapterMenu: each on: html]]]
  388. ! !
  389. !DocumentationWidget methodsFor: 'updating'!
  390. updateChapterDiv
  391. chapterDiv contents: [:html |
  392. html with: self selectedChapter]
  393. ! !
  394. !DocumentationWidget class methodsFor: 'instance creation'!
  395. on: aBuilder
  396. ^self new
  397. builder: aBuilder;
  398. yourself
  399. ! !
  400. DocChapter subclass: #ClassesIndexChapter
  401. instanceVariableNames: ''
  402. category: 'Documentation'!
  403. !ClassesIndexChapter methodsFor: 'accessing'!
  404. cssClass
  405. ^'index_doc ', super cssClass
  406. !
  407. title
  408. ^'Smalltalk classes by index'
  409. !
  410. alphabet
  411. ^'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
  412. ! !
  413. !ClassesIndexChapter methodsFor: 'rendering'!
  414. renderDocOn: html
  415. html h1 with: self title.
  416. self alphabet do: [:letter || classes |
  417. classes := Smalltalk current classes select: [:each | each name first = letter].
  418. classes ifNotEmpty: [html h2 with: letter].
  419. html ul with: [
  420. (classes sorted: [:a :b | a name < b name])
  421. do: [:each |
  422. html li with: [html a
  423. with: each name;
  424. onClick: [self selectClass: each]]]]]
  425. ! !
  426. !ClassesIndexChapter class methodsFor: 'accessing'!
  427. on: aClass
  428. ^self new
  429. theClass: aClass;
  430. yourself
  431. ! !
  432. Object subclass: #ClassSelectionAnnouncement
  433. instanceVariableNames: 'theClass'
  434. category: 'Documentation'!
  435. !ClassSelectionAnnouncement methodsFor: 'accessing'!
  436. theClass
  437. ^theClass
  438. !
  439. theClass: aClass
  440. theClass := aClass
  441. ! !
  442. !ClassSelectionAnnouncement class methodsFor: 'instance creation'!
  443. on: aClass
  444. ^self new
  445. theClass: aClass;
  446. yourself
  447. ! !